Nohttp的链接 点击下载源码
框架特性:流行http库
1、动态配置底层框架为OkHttp、HttpURLConnection,支持异步请求、支持同步请求。
2、多文件上传,支持大文件上传,表单提交数据,文件下载、上传下载、上传和下载的进度回调、错误回调
3、支持Json、xml、Map、List的提交
4、完美的Http缓存模式,可指定缓存到数据库、SD卡,缓存数据已安全加密
5、在6.0以上手机缓存到SD卡时需要请求运行时权限:AndPermission
6、自定义Request,直接请求JsonObject、JavaBean等
7、Cookie的自动维持,App重启、关开机后还持续维持
8、http 301 302 303 304 307重定向,支持多层嵌套重定向
9、Https、自签名网站Https的访问、支持双向验证,失败重试机制,支持请求优先级
10、GET、POST、PUT、PATCH、HEAD、DELETE、OPTIONS、TRACE等请求协议
11、用队列保存请求,平均分配多线程的资源,支持多个请求并发
12、支持取消某个请求、取消指定多个请求、取消所有请求
使用方法
如果要使用OkHttp作为网络层,请再依赖:
compile 'com.yolanda.nohttp:nohttp:1.1.0'
compile 'com.yanzhenjie.nohttp:okhttp:1.1.0'
初始化
NoHttp初始化需要一个Context,最好在Application的onCreate()中初始化,记得在manifest.xml中注册Application。
一般初始化
直接初始化后,一切采用默认设置。
NoHttp.initialize(this);
高级自定义初始化
超时配置,默认10s
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卡
.setCacheStore(
new DiskCacheStore(this)
)
);
配置Cookie保存的位置,默认保存在数据库
NoHttp.initialize(this, new NoHttp.Config()
...
// 默认保存数据库DBCookieStore,开发者可以自己实现。
.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非常整齐。
所以说,如果你使用过程中遇到什么问题了,开启调试模式,一切妖魔鬼怪都会现形的。
请求队列(请求类型)
常用的请求方式String请求
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);
requestQueue.add(0, request, listener);
Json请求
// 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);
Bitmap请求
Request<Bitmap> request = NoHttp.createImageRequest(url, RequestMethod.DELETE);
requestQueue.add(0, request, listener);
代码实现:
1、清单文件注册
<application
android:name=".view.MyApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
MyApp
**
* 获取上下文,配置网络信息
*/
/**获取上下文的方法**/
public class MyApp extends Application {
private static Context sContext;
//获取当前上下文
public static Context getContext() {
return sContext;
}
@Override
public void onCreate() {
super.onCreate();
sContext=this;
//1.最简单配置
NoHttp.initialize(this);
//2.高级配置
NoHttp.Config config=new NoHttp.Config()
.setConnectTimeout(5000)//如果5000没有完成连接成功,抛出异常
.setNetworkExecutor(new OkHttpNetworkExecutor());//使用okhttp请求网络
NoHttp.initialize(this,config);//1上下文 2.配置
//友好调试
Logger.setDebug(true);//进入调试模式 ,可以到打印Log
Logger.setTag("Nohttp");
}
/**
* 获取网络数据
*/
private void getData() {
String url = "http://www.baidu.com";
//创建消息请求队列
RequestQueue queue = NoHttp.newRequestQueue();
//创建String请求类型
StringRequest request = (StringRequest) NoHttp.createStringRequest(url, RequestMethod.GET);//1.连接地址 2.请求方法
//设置缓存模式
request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
//实现回调接口
OnResponseListener<String> callback = new OnResponseListener<String>() {
@Override
public void onStart(int what) {
//开始请求数据
}
@Override
public void onSucceed(int what, Response<String> response) {
//请求数据成功
String xml = response.get();//获取json数据或者xml数据
}
@Override
public void onFailed(int what, Response<String> response) {
//请求数据失败
}
@Override
public void onFinish(int what) {
//请求数据结束
}
};
queue.add(0, request, callback);
}
进行网络工具封装
public class NetContentUtil {
private static NetContent netContent;
private NetContent(){
queue = getClient();
}
//单例模式
public static NetContent getInstance(){
if(netContent==null){
netContent=new NetContent();
}
return netContent;
}
private RequestQueue queue;
private RequestQueue getClient(){
//获取nohttp实例
RequestQueue queue = NoHttp.newRequestQueue();
return queue;
}
public void doGet(String url,final ContentCallBack callBack){
StringRequest request = (StringRequest) NoHttp.createStringRequest(url, RequestMethod.GET);//1.连接地址 2.请求方法
//设置缓存模式
request.setCacheMode(CacheMode.REQUEST_NETWORK_FAILED_READ_CACHE);
OnResponseListener<String> callback = new OnResponseListener<String>(){
@Override
public void onStart(int what) {
callBack.onStart(what);
}
@Override
public void onSucceed(int what, Response<String> response) {
String xml = response.get();
byte[] charXml = xml.getBytes();
callBack.onSucceed(charXml);
}
@Override
public void onFailed(int what, Response<String> response) {
callBack.onFailed(what,response);
}
@Override
public void onFinish(int what) {
callBack.onFinish(what);
}
};
queue.add(0, request, callback);
}
}
实现自定义接口
public interface ContentCallBack {
void onStart(int what);
void onSucceed( byte[] charXml);
void onFailed(int what, Response<String> response);
void onFinish(int what);
}
提取成ApiUrl类,方便更改网络参数
public class ApiUrl {
public static final String HOST="http://192.168.110.110:8080";
public static final String MYPAGER=HOST+"/my_information.xml";
}