官方文档:http://doc.nohttp.net/222880
跟着官网集成了下nohttp
1.初始化
定义自己的Applicastion
NoHttp.Config config = new NoHttp.Config();
config.setConnectTimeout(12000);
config.setReadTimeout(12000);
config.setCacheStore(new DBCacheStore(this).setEnable(true));//设置数据库缓存
config.setCookieStore(new DBCookieStore(this).setEnable(true));//设置cookie
config.setNetworkExecutor(newURLConnectionNetworkExecutor());//设置网络层是HttpURLConnection//还可以设置网络层的底层是okhtp
NoHttp.initialize(this,config); //初始化
//设置测试的log日志打印
Logger.setDebug(true);
Logger.setTag("NoHttpSample");
2.同步请求
(1)拿到Request<T> 请求对象,T可以设定为我们json结果可转化的实体类,则监听时同样设置该类,参照图片的获取
Request<String> request = NoHttp.createStringRequest(url,RequestMethod.GET);// 同步请求、、、、、、、、、、、、、、、、、、、、、、、、、、、、、请求方式不同可能请求不出来
// Request<String> request2=newStringRequest(url, RequestMethod.POST);//方式二获取request
(2)设置参数
request.add("wd", "琴吹柚");
(3)开始请求
Response<String> response = NoHttp.startRequestSync(request);
Int responseCode=response.responseCode();//请求结果码
String result= response.get();//请求结果
注意:上面的这些都需要在子线程中运行
结果我们可以使用handler传递到主线程
3.异步请求
(1)拿到请求队列
任意添加多个请求到队列时,使用同一个Listener接受结果,listener的任何一个方法被回调时都会返回在添加请求到队列时写的相应what值,可以用这个what来区分是哪个请求的回调结果,你可以理解为它的作用和handler的what一样的作用,就是用来区分信号来源的。
// RequestQueue queue =NoHttp.newRequestQueue(1);
// RequestQueue queue2 =NoHttp.newRequestQueue(2);// 设置并发值是2
RequestQueue queue = NoHttp.newRequestQueue();//默认并发值是3
(2)拿到Request<T> 请求对象,
Request<String> request = newStringRequest(url);// 这种方式传递的参数有误
// Request<String> request=NoHttp.createStringRequest(url);//方式二获取request
(3)设置参数
request.add("wd", "琴吹柚");
(4)添加请求到队列中
queue.add(1, request, newOnResponseListener<String>() {
@Override
publicvoid onFailed(int what, Response<String> response) {
Exceptionexception = response.getException();// 获取失败原因
if(exception instanceof NetworkError) {// 网络不好。
}
Toast.makeText(MainActivity.this,what + "网络请求失败",
Toast.LENGTH_SHORT).show();
}
@Override
publicvoid onFinish(int arg0) {
//网络请求结束,无论失败还是成功都会回调
//可以关闭请求
progressDialog.dismiss();
}
@Override
publicvoid onStart(int arg0) {
//开始请求,可以设置进度
progressDialog= ProgressDialog.show(MainActivity.this,
"查询数数据中","请稍等...", true, false);
}
@Override
publicvoid onSucceed(int arg0, Response<String> response) {
if(response.responseCode() == 200) {// 请求成功。
Stringresult = response.get();
show_data.loadDataWithBaseURL(null,result, "text/html",
"utf-8",null);
}
}
});
注意队列的意义:
- 当一个页面初始化时要请求多个接口,那么耗时相对会更长,对于用户的体验是很差的,所以如果能同时执行多个请求,那么将会缩短网络请求的时间。
- 假如我们传入的并发值是3,但是我们同时添加了5个请求到队列,第四个请求将会在前三个请求的任何一个完成后被发起,以此类推...
- 假如我们想让请求一个个执行,那么我们讲队列设置为1个并发,我们连续添加了10个请求到队列,这个10个队列将会按照添加顺序依次执行。如果要设置请求优先级,请往下看。
4.封装
根据文档封装
public classCallServer {
privatestatic CallServer instance;
/**
* 请求队列。
*/
publicsynchronized static CallServer getInstance() {
if (instance == null)
synchronized (CallServer.class) {
if (instance == null)
instance = newCallServer();
}
return instance;
}
/**
* 请求队列。
*/
privateRequestQueue requestQueue;
privateCallServer() {
requestQueue = NoHttp.newRequestQueue(3);//异步请求队列数。
}
/**
* 添加一个请求到请求队列。
* @param what 用来标志请求, 当多个请求使用同一个Listener时, 在回调方法中会返回这个what。
* @param request 请求对象。
* @param listener 结果回调对象。
*/
public<T> void add(int what, Request<T> request, OnResponseListenerlistener) {
requestQueue.add(what, request, listener);
}
/**
* 取消这个sign标记的所有请求。
* @param sign 请求的取消标志。
*/
publicvoid cancelBySign(Object sign) {
requestQueue.cancelBySign(sign);
}
/**
* 取消队列中所有请求。
*/
publicvoid cancelAll() {
requestQueue.cancelAll();
}
}
5.请求图片
private voidgetImage(){
Request<Bitmap>request=NoHttp.createImageRequest("http://b.hiphotos.baidu.com/zhidao/pic/item/d833c895d143ad4b08c2629580025aafa50f06f2.jpg",RequestMethod.GET);
request(0,request, new OnResponseListener<Bitmap>() {
@Override
publicvoid onFailed(int what, Response<Bitmap> response) {
Exceptionexception = response.getException();// 获取失败原因
if(exception instanceof NetworkError) {// 网络不好。
}
Toast.makeText(MainActivity.this,what + "网络请求失败",
Toast.LENGTH_SHORT).show();
}
@Override
publicvoid onFinish(int what) {
progressDialog.dismiss();
progressDialog=null;
}
@Override
publicvoid onStart(int what) {
if(progressDialog == null) {
progressDialog= ProgressDialog.show(MainActivity.this,
"查询数数据中","请稍等...", true, false);
}
}
@Override
publicvoid onSucceed(int what, Response<Bitmap> response) {
if(response.responseCode() == 200) {// 请求成功。
Bitmapbitmap = response.get();
show_image.setImageBitmap(bitmap);
}
}
});
}