您的位置:首页 > 理论基础 > 计算机网络

Android中NoHttp使用与简单封装

2018-02-28 21:59 357 查看

介绍

NoHttp是一个开源的Android网络框架,代码托管在Github。NoHttp最早是使用Httpclient做了些简单的封装,只是在公司内部使用。后来随着Android弃用了Httpclient后,NoHttp换用HttpURLConnection做了封装,直到Android6.0时Android SDK删除了HttpClient的api后才真正的有了NoHttp。于是做了大量的重构工作,把NoHttp从项目中独立出来,做了开源。

先看看NoHttp的特性 1:

1.多种请求方式并发,调用,支持get,post,等网络解析方式

2.文件上传,文件下载,下载进度回调,错误回调

3.支持取消某个请求,取消指定多个请求,取消所有请求

4.支持自定义Request,利用NoHttp泛型可以解析成你想要的任何数据格式(String,Json,JavaBean等)

先看看NoHttp的特性 2:

1.支持请求String,Json,FastJson,Gson,Bitmap,JavaBean,XML等扩展。异步请求。拿到结果直接更新UI,支持同步请球。

2.大文件上传不会发生OOM。支持File,InputStream,Bitmap,实现NoHttp的binary接口,一般情况任何东西都可以传。

3.文件下载,支持多个文件同时下载,并且有进度回调,错误回调,支持暂停继续下载,支持取消下载

流程分析



基本使用

在studio中进行依赖关联

compile ‘com.yolanda.nohttp:nohttp:1.0.4’

在application中实现初始化

在Application中onCreate的进行 NoHttp.initialize(this);

在Manifest修改application android:name

添加相应的权限

<uses-permission android:name="android.permission.READEXTERNALSTORAGE" />
<uses-permission android:name="android.permission.WRITEEXTERNALSTORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESSNETWORKSTATE" />
<uses-permission android:name="android.permission.ACCESSWIFISTATE" />


新建一个队列用来添加消息请求,可以并发(一起发送,一起请求)多个消息请求,默认为3个发送消息请求,并添加到队列中设置结果回调监听,对请求结果进行统一处理

GET 方式请求

String url = "http://www.baidu.com";
//1.创建一个队列
RequestQueue queue = NoHttp.newRequestQueue();
//3.创建消息请求
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);
//2.利用队列去添加消息请求
/***
* what:请求的标识
* request:请求
* response:请求的回调监听
*/

/**请求可以并发,统一处理响应结果**/
queue.add(0, request, new OnResponseListener<String>() {
//请求一开始的回调,对话框的加载
@Override
public void onStart(int what) {
}
//成功之后的回调
@Override
public void onSucceed(int what, Response<String> response) {
//设置响应结果
mTv.setText(response.get());

}
//网络请求失败的回调
@Override
public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
}
//网络请求完成
@Override
public void onFinish(int what) {
}

});


POST请求

POST请求就是在创建消息请求的时候把RequestMethod.GET换成RequestMethod.POST

然后

request.add("username", "admin");
request.add("password", "123456");


当然也可以添加头部

request.addHeader("xxid","123456abc");
request.addHeader("xxkey","123456abc");


简单封装

自定义 HttpResponseListener 实现 OnResponseListener

public class HttpResponseListener<T> implements OnResponseListener<T> {
private final Request<?> request;
private HttpListner<T> listener;
private WaitDialog dialog;
private boolean isLoading;
public HttpResponseListener(HttpListner<T> listener, Context mcontext, boolean canCel, final Request<?> request, boolean isLoading) {
this.request = request;
this.listener = listener;
this.isLoading = isLoading;
if (mcontext != null && !isLoading) {
dialog = new WaitDialog(mcontext);
dialog.setCancelable(canCel);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
request.cancel();
}
});
}
}

@Override
public void onStart(int what) {
if (dialog != null && !isLoading) {
dialog.show();
}
}

@Override
public void onSucceed(int what, Response<T> response) {
if (listener != null) {
listener.onSucceed(what, response);
}
}

@Override
public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
if (listener != null) {
listener.onFailed(what, url, tag, exception.getMessage(), responseCode, networkMillis);
}
}

@Override
public void onFinish(int what) {
if (isLoading && dialog != null && dialog.isShowing() ) {
dialog.dismiss();
}
}
}


接口

public interface HttpListner<T> {
void onSucceed(int what, Response<T> response);
void onFailed(int what, String url, Object tag, String message, int responseCode, long networkMillis);
}


自定义对话框

public class WaitDialog extends ProgressDialog {
public WaitDialog(Context context, int theme) {
super(context, theme);
}
public WaitDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCanceledOnTouchOutside(false);
setProgressStyle(STYLE_SPINNER);
setMessage("正在加载。。");
}
}


-封装

public class CallServer {
private volatile static CallServer mcallServer;
private final RequestQueue queue;
private CallServer() {
queue = NoHttp.newRequestQueue();
}
public static CallServer getRequestInstace() {
if (mcallServer == null) {
synchronized (CallServer.class) {
if (mcallServer == null) {
mcallServer = new CallServer();
}
}
}
return mcallServer;
}
public <T> void add(int what, Request<T> request, HttpListner<T> httpListner, Context mcontext, boolean canCel, boolean isLoading) {
if (queue != null) {
queue.add(what, request, new HttpResponseListener<T>(httpListner, mcontext, canCel, request, isLoading));
}
}
public void cancelAll() {
if (queue != null) {
queue.cancelAll();
}
}
public void cancelBySign(Object object) {
if (queue != null) {
queue.cancelBySign(object);
}
}
}


使用

public class MainActivity extends AppCompatActivity implements HttpListner<String> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showMessage(View view) {
String url = "http://www.baidu.com";
//1.创建一个队列
RequestQueue queue = NoHttp.newRequestQueue();
//3.创建消息请求
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);
//2.利用队列去添加消息请求
CallServer.getRequestInstace().add(0, request, this, this, false, false);
}
@Override
public void onSucceed(int what, Response<String> response) {
if (what == 0) {
Toast.makeText(this, response.get(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailed(int what, String url, Object tag, String message, int responseCode, long networkMillis) {
if (what == 0) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: