标签:
开源地址https://github.com/yanzhenjie/NoHttp
项目倒入
compile ‘com.yolanda.nohttp:nohttp:1.0.5‘需要的权限
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.INTERNET"/><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>初始化
@Overridepublicvoid onCreate(){super.onCreate();// 初始化NoHttpNoHttp.init(this);// 开启调试模式Logger.setDebug(true);Logger.setTag("NoHttpSample");}使用详情可参考官方文档 http://api.nohttp.net/
publicclassHttpResponseListener<T>implementsOnResponseListener<T>{/** * Dialog */privateWaitDialog mWaitDialog;/** * 当前请求 */privateRequest<T> mRequest;/** * 结果回调 */privateHttpListener<T> callback;/** * 是否显示dialog */privateActivity context;/** * @param context context用来实例化dialog * @param request 请求对象 * @param httpCallback 回调对象 * @param canCancel 是否允许用户取消请求 * @param isLoading 是否显示dialog */publicHttpResponseListener(Activity context,Request<T> request,HttpListener<T> httpCallback,boolean canCancel,boolean isLoading){this.context = context;this.mRequest = request;if(isLoading){// 需要显示dialog mWaitDialog =newWaitDialog(context); mWaitDialog.setCancelable(canCancel); mWaitDialog.setOnCancelListener(newDialogInterface.OnCancelListener(){@Overridepublicvoid onCancel(DialogInterface dialog){ mRequest.cancel();// dialog被用户关闭时, 取消当前请求}});}this.callback = httpCallback;this.isLoading = isLoading;}/** * 开始请求, 这里显示一个dialog */@Overridepublicvoid onStart(int what){if(!context.isFinishing && mWaitDialog !=null&&!mWaitDialog.isShowing()) mWaitDialog.show();}/** * 结束请求, 这里关闭dialog */@Overridepublicvoid onFinish(int what){if(mWaitDialog !=null&& mWaitDialog.isShowing()) mWaitDialog.dismiss();}/** * 成功回调 */@Overridepublicvoid onSucceed(int what,Response<T> response){if(callback !=null) callback.onSucceed(what, response);}/** * 失败回调 */@Overridepublicvoid onFailed(int what,Response<T> response){if(callback !=null) callback.onFailed(what, response);}}publicinterfaceHttpListener<T>{/** * 请求失败 */void onSucceed(int what,Response<T> response);/** * 请求成功 */void onFailed(int what,Response<T> response);}看到这里有人可能开始有点迷惑了,不是需要OnResponseListener对象嘛,现在HttpListener怎么用啊?可观别急,我们继续看下面请求入口的封装。
因为NoHttp是队列的请求方式,方便开发者控制并发和线程数量,而NoHttp生成队列都是newInstance,每次都是一个新的队列对象,所以生成的Queue都需要做单列,请看:
publicclassCallServer{privatestaticCallServer callServer;/** * 请求队列 */privateRequestQueue requestQueue;privateCallServer(){ requestQueue =NoHttp.newRequestQueue();}/** * 请求队列 */publicsynchronizedstaticCallServer getRequestInstance(){if(callServer ==null) callServer =newCallServer();return callServer;}/** * 添加一个请求到请求队列 * * @param context context用来实例化dialog * @param what 用来标志请求,在回调方法中会返回这个what,类似handler的what * @param request 请求对象 * @param callback 结果回调对象 * @param canCancel 是否允许用户取消请求 * @param isLoading 是否显示dialog */public<T>void add(Context context,int what,Request<T> request,HttpListener<T> callback,boolean canCancel,boolean isLoading){ requestQueue.add(what, request,newHttpResponseListener<T>(context, request, callback, canCancel, isLoading));}/** * 取消这个sign标记的所有请求 */publicvoid cancelBySign(Object sign){ requestQueue.cancelBySign(sign);}/** * 取消队列中所有请求 */publicvoid cancelAll(){ requestQueue.cancelAll();}/** * 退出app时停止所有请求 */publicvoid stopAll(){ requestQueue.stop();}}/** * 发起请求 */privatevoid requestString(){Request<String> request =NoHttp.createStringRequest(Constants.URL_NOHTTP_CACHE_STRING);CallServer.getRequestInstance().add(this,0, request, httpListener,false,true);}/** * 接受响应 */privateHttpListener<String> httpListener =newHttpListener<String>(){@Overridepublicvoid onSucceed(int what,Response<String> response){// 拿到请求结果String result = response.get();...}@Overridepublicvoid onFailed(int what,Response<String> response){Toast.show("请求失败");}};标签:
原文地址:http://www.cnblogs.com/wisemen/p/5836839.html