支持与RxJava
完美结合、支持一句话切换底层为OkHttp
,支持缓存数据到数据库或SD卡(缓存数据自动加密)支持请求Restful风格的接口,比Retrofit更简单易用。
NoHttp使用文档:doc.nohttp.net
NoHttp测试接口:api.nohttp.net
欢迎加入QQ技术交流群:46523908
Demo中用的6.0权限管理友链:https://github.com/yanzhenjie/AndPermission
比Retrofit使用更简单、更易用。
compile 'com.yanzhenjie.nohttp:nohttp:1.1.1'
compile 'com.yanzhenjie.nohttp:okhttp:1.1.1'
新版NoHttp修改了包名为
com.yanzhenjie.nohttp
,开发者从旧版升级后请使用全局替换,将com.yolanda.nohttp
替换为com.yanzhenjie.nohttp
即可。
NoHttp初始化需要一个Context,最好在Application
的onCreate()
中初始化,记得在manifest.xml
中注册Application
。
直接初始化后,一切采用默认设置。
NoHttp.initialize(this);
NoHttp.initialize(this, new NoHttp.Config()
// 设置全局连接超时时间,单位毫秒
.setConnectTimeout(30 * 1000)
// 设置全局服务器响应超时时间,单位毫秒
.setReadTimeout(30 * 1000)
);
NoHttp.initialize(this, new NoHttp.Config()
...
.setCacheStore(
// 保存到数据库
new DBCacheStore(this).setEnable(true) // 如果不使用缓存,设置false禁用。
// 或者保存到SD卡:new DiskCacheStore(this)
)
);
NoHttp.initialize(this, new NoHttp.Config()
...
// 默认保存数据库DBCookieStore,开发者也可以自己实现CookieStore接口。
.setCookieStore(
new DBCookieStore(this).setEnable(false) // 如果不维护cookie,设置false禁用。
)
);
NoHttp.initialize(this, new NoHttp.Config()
...
// 使用HttpURLConnection
.setNetworkExecutor(new URLConnectionNetworkExecutor())
// 或者使用OkHttp
// .setNetworkExecutor(new OkHttpNetworkExecutor())
);
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Logger.setDebug(true);// 开启NoHttp的调试模式, 配置后可看到请求过程、日志和错误信息。
Logger.setTag("NoHttpSample");// 设置NoHttp打印Log的tag。
开启NoHttp的调试模式后可看到请求过程、日志和错误信息,基本不用抓包。可以看到请求头、请求数据、响应头、Cookie等,而且打印出的Log非常整齐。
所以说,如果你使用过程中遇到什么问题了,开启调试模式,一切妖魔鬼怪都会现形的。
##第三方异步框架
RxJava
其实核心就是集合RxJava
的异步功能和NoHttp
的同步功能:
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.DELETE);
Response<String> response = NoHttp.startRequestSync(request);
if (response.isSucceed()) {
// 请求成功
} else {
// 请求失败
}
把上面的代码利用RxJava
封装起来就OK了,这两个项目可以作为参考:
RequestQueue requestQueue = NoHttp.newRequestQueue();
// 如果要指定并发值,传入数字即可:NoHttp.newRequestQueue(3);
// 发起请求
requestQueue.add(what, request, responseListener);
responseLisetener
响应时回调给开发者,所以开发者可以用一个responseLisetener
接受多个请求的响应,用what来区分结果。而不用像有的框架一样,每一个请求都要new一个callback。##请求类型
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);
requestQueue.add(0, request, listener);
// JsonObject
Request<JSONObject> objRequest = NoHttp.createJsonObjectRequest(url, RequestMethod.POST);
requestQueue.add(0, objRequest, listener);
// JsonArray
Request<JSONArray> arrayRequest = NoHttp.createJsonArrayRequest(url, RequestMethod.PUT);
requestQueue.add(0, arrayRequest, listener);
Request<Bitmap> request = NoHttp.createImageRequest(url, RequestMethod.DELETE);
requestQueue.add(0, request, listener);
// FastJson
Request<JSONObject> request = new FastJsonRequest(url, RequestMethod.POST);
requestQueue.add(0, request, listener);
// 内部使用Gson、FastJson解析成JavaBean
Request<UserInfo> request = new JavaBeanRequest(url, RequestMethod.GET);
requestQueue.add(0, request, listener);
Request<JSONObject> request = new JavaBeanRequest(url, RequestMethod.POST);
.add("name", "yoldada") // String类型
.add("age", 18) // int类型
.add("sex", '0') // char类型
.add("time", 16346468473154) // long类型
// 添加Bitmap
.add("head", new BitmapBinary(bitmap))
// 添加File
.add("head", new FileBinary(file))
// 添加ByteArray
.add("head", new ByteArrayBinary(byte[]))
// 添加InputStream
.add("head", new InputStreamBinary(inputStream));
文件上传实现了http表单的标准协议,满足了广大开发者的需求,有以下几种形式:
Request<String> request = ...
request.add("file", new FileBinary(file));
Request<String> request = ...
request.add("file1", new FileBinary(File));
request.add("file2", new FileBinary(File));
request.add("file3", new InputStreamBinary(InputStream));
request.add("file4", new ByteArrayBinary(byte[]));
request.add("file5", new BitmapBinary(Bitmap));
Request<String> request = ...
fileList.add("image", new FileBinary(File));
fileList.add("image", new InputStreamBinary(InputStream));
fileList.add("image", new ByteArrayBinary(byte[]));
fileList.add("image", new BitmapBinary(Bitmap));
或者:
Request<String> request = ...
List<Binary> fileList = ...
fileList.add(new FileBinary(File));
fileList.add(new InputStreamBinary(InputStream));
fileList.add(new ByteArrayBinary(byte[]));
fileList.add(new BitmapStreamBinary(Bitmap));
request.add("file_list", fileList);
提交Body分为提交Json、提交String、提交Xml、提交流等,具体用法如下:
// 提交普通String
request.setDefineRequestBody(String, ContentType);
// 提交json字符串
request.setDefineRequestBodyForJson(JsonString)
// 提交jsonObject对象,其实还是json字符串
request.setDefineRequestBodyForJson(JSONObject)
// 提交xml字符串
request.setDefineRequestBodyForXML(XmlString)
// 提交字体Body,比如File(这跟表单上传不一样的),可以转为InputStream来提交
request.setDefineRequestBody(InputStream, ContentType)
在当前线程发起请求,在线程这么使用。
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.DELETE);
Response<String> response = NoHttp.startRequestSync(request);
if (response.isSucceed()) {
// 请求成功
} else {
// 请求失败
}
NoHttp的缓存非常强大,支持缓存到数据库、换到SD卡等,并且不论缓存在数据库或者SD,NoHttp都把数据进行了加密,需要在初始化的时候配置缓存的位置。
需要注意的是,在6.0以上的手机中如果要缓存在SD卡,需要在请求之前,需要请求运行时权限,如果你不懂运行时权限,可以看这个项目:AndPermission。
NoHttp.initialize(this, new NoHttp.Config()
...
// 保存到数据库
.setCacheStore(
new DBCacheStore(this).setEnable(true) // 如果不使用缓存,设置false禁用。
)
// 或者保存到SD卡
.setCacheStore(
new DiskCacheStore(this)
)
);
Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
request.setCacheMode(CacheMode.DEFAULT);
Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
请求String,缓存String:
Request<JSONObject> request = NoHttp.createJsonObjectRequest(url);
// 非标准Http协议,改变缓存模式为IF_NONE_CACHE_REQUEST_NETWORK
request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST_NETWORK);
请求图片,缓存图片:
Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.IF_NONE_CACHE_REQUEST_NETWORK);
Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);
...
Request<Bitmap> request = NoHttp.createImageRequest(imageUrl);
request.setCacheMode(CacheMode.ONLY_READ_CACHE);
因为下载文件代码比较多,这里贴关键部分,具体的请参考demo。
文件下载也是队列,队列和开头所说的请求的队列是一样的。
//下载文件
downloadRequest = NoHttp.createDownloadRequest...
// what 区分下载
// downloadRequest 下载请求对象
// downloadListener 下载监听
downloadQueue.add(0, downloadRequest, downloadListener);
downloadRequest.cancel();
private DownloadListener downloadListener = new DownloadListener() {
@Override
public void onStart(int what, boolean resume, long preLenght, Headers header, long count) {
// 下载开始
}
@Override
public void onProgress(int what, int progress, long downCount, long speed) {
// 更新下载进度和下载网速
}
@Override
public void onFinish(int what, String filePath) {
// 下载完成
}
@Override
public void onDownloadError(int what, StatusCode code, CharSequence message) {
// 下载发生错误
}
@Override
public void onCancel(int what) {
// 下载被取消或者暂停
}
};
NoHttp支持取消某个请求、取消指定多个请求、取消所有请求。
request.cancel();
request1.setCancelSign(sign);
request2.setCancelSign(sign);
...
// 取消队列中多个用sign标志的请求
queue.cancelBySign(sign);
queue.cancelAll();
队列停止后再添加请求到队列后,请求不会被执行。
RequestQueue queue = NoHttp.newRequestQueue();
...
queue.stop();
NoHttp的所有自带请求都是继承RestRequest
类,所以我们自定义请求也需要继承RestRequest
,泛型写自己想要请求的数据类型,最后在parseResponse()
方法中解析服务器数据成自己自己想要的数据类型即可。
public class FastJsonRequest extends RestRequestor<JSONObject> {
public FastJsonRequest(String url) {
this(url, RequestMethod.GET);
}
public FastJsonRequest(String url, RequestMethod requestMethod) {
super(url, requestMethod);
}
@Override
public JSONObject parseResponse(Headers header, byte[] body) throws Throwable {
String result = StringRequest.parseResponseString(headers, body);
return JSON.parseObject(result);
}
}
public class JavaBeanRequest<T> extends RestRequest<T> {
private Class<T> clazz;
public JavaBeanRequest(String url, Class<T> clazz) {
this(url, RequestMethod.GET, clazz);
}
public JavaBeanRequest(String url, RequestMethod requestMethod, Class<T> clazz) {
super(url, requestMethod);
this.clazz = clazz;
}
@Override
public T parseResponse(Headers header, byte[] body) throws Throwable {
String response = StringRequest.parseResponseString(header, body);
// 这里如果数据格式错误,或者解析失败,会在失败的回调方法中返回 ParseError 异常。
return JSON.parseObject(response, clazz);
}
}
// 使用FastJson自定义请求
Request<JSONObject> request = new FastJsonRequest(url, requestMethod);
queue.add(what, mRequest, listener);
...
// 直击请求JavaBean
Request<UserInfo> request = new JavaBeanRequest(url, UserInfo.class);
queue.add(what, request, listener);
NoHttp设计到兼容高版本系统的api采用反射调用,所以所有类都可以被混淆,如果你非要keep的话,如下配置即可。
-dontwarn com.yanzhenjie.nohttp.**
-keep class com.yanzhenjie.nohttp.**{*;}
// nohttp
-dontwarn com.yanzhenjie.nohttp.**
-keep class com.yanzhenjie.nohttp.**{*;}
// nohttp-okhttp
-dontwarn com.yanzhenjie.nohttp.**
-keep class com.yanzhenjie.nohttp.**{*;}
// okhttp
-dontwarn okhttp3.**
-keep class okhttp3.** { *;}
-dontwarn okio.**
-keep class okio.** { *;}
Copyright 2016 Yan Zhenjie
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )