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

android请求网络——第三方库android-async-http的使用(4)

2016-04-18 16:51 676 查看
前言:

        当我再写这个系列的第四篇时,距离上一篇已经很久了,还记得准备写第一篇的时候,打算系统的介绍一下这个优秀的网络请求类库,甚至弄点源码出来一起看看,没想到还是犯懒了,前段时间,这个系列的前三篇陆陆续续被收录到知识库,把这个系列写完的想法也浮出水面。

        android-async-http是一个很优秀的类库,虽然相比于当前一些也很热门的库,他的使用显得略麻烦了一些(毕竟笔者没有真正的去做系统的比较,这里就不合okhttp等库进行一个对比)。之所以说优秀,是因为它纯粹,你可以将它的功能定义局限在网络模块,只负责发起请求和“提供”请求结果,这种“提供”是通过回调接口处理的。它可以替你转变实体,也可以单纯的给你body里面的byte[]。纯粹就意味着可以做更出色的分层。

        当前框架横行,一般来说,一些特定需求的库,如图片加载库(例如picasso,glide),都会依赖第三方网络请求库。做移动平台客户端程序的时候,使用android-async-http这样的库更多的是为了做web-api的请求。

        所以说,单人开发时使用其他的使用注解形式简化model解析、ui更新会提升效率,多人协同开发时还是用android-async-http这样的类库更合适一些,每个人负责相对独立的层(模块),和他人的工作、只要接口标准定义好了、就不需要过度的介入,bug也很容易定位。

        说了这么多,这一篇的主题是丰富一下使用aah(以下使用aah代指android-async-http)进行post请求

正文:

我们先看一下aah中提供的post请求的函数原型 public RequestHandle post(String url, ResponseHandlerInterface responseHandler);

public RequestHandle post(String url, RequestParams params, ResponseHandlerInterface responseHandler);

public RequestHandle post(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler);

public RequestHandle post(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler);

public RequestHandle post(Context context, String url, Header[] headers, RequestParams params, String contentType, ResponseHandlerInterface responseHandler);

public RequestHandle post(Context context, String url, Header[] headers, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler);
这一系列的重载看不出啥名堂,再加一段代码:
public RequestHandle post(String url, ResponseHandlerInterface responseHandler) {
return this.post((Context)null, url, (RequestParams)null, responseHandler);
}

public RequestHandle post(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
return this.post((Context)null, url, params, responseHandler);
}

public RequestHandle post(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) {
return this.post(context, url, this.paramsToEntity(params, responseHandler), (String)null, responseHandler);
}

public RequestHandle post(Context context, String url, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
return this.sendRequest(this.httpClient, this.httpContext, this.addEntityToRequestBase(new HttpPost(this.getURI(url)), entity), contentType, responseHandler, context);
}

public RequestHandle post(Context context, String url, Header[] headers, RequestParams params, String contentType, ResponseHandlerInterface responseHandler) {
HttpPost request = new HttpPost(this.getURI(url));
if(params != null) {
request.setEntity(this.paramsToEntity(params, responseHandler));
}

if(headers != null) {
request.setHeaders(headers);
}

return this.sendRequest(this.httpClient, this.httpContext, request, contentType, responseHandler, context);
}

public RequestHandle post(Context context, String url, Header[] headers, HttpEntity entity, String contentType, ResponseHandlerInterface responseHandler) {
HttpEntityEnclosingRequestBase request = this.addEntityToRequestBase(new HttpPost(this.getURI(url)), entity);
if(headers != null) {
request.setHeaders(headers);
}

return this.sendRequest(this.httpClient, this.httpContext, request, contentType, responseHandler, context);
}
我们可以发现类库做了一件事情,将“键值对形式”的params转成了HttpEntity,具体怎么转的就不扒了,转变出错会调用回调接口中的onFailure
通过上面的代码窥一斑,可以推测aah的功能了,可以偷懒的使用一大堆默认,也可以自己拼header、拼body。

拼header:

库中定义了接口:

public interface Header {
String getName();

String getValue();

HeaderElement[] getElements() throws ParseException;
}
public interface HeaderElement {
String getName();

String getValue();

NameValuePair[] getParameters();

NameValuePair getParameterByName(String var1);

int getParameterCount();

NameValuePair getParameter(int var1);
}
public interface NameValuePair {
String getName();

String getValue();
}
笔者并没有去拼自定义的header,后端都放到body里面去了,至于这里面都多少道道就不是很清楚了

拼HttpEntity:

我们知道这里有form-data,x-www-form-urlencoded,raw,binary四种类型,前两种不需要自己弄了,用RequestParam直接转就好了,raw形式的需要指定contentType,我们看到函数需要该参数,以拼一个json的为例:

JSONObject jObj = new JSONObject();
String value = "VALUE";
jObj.put("key", value);
StringEntity ret = null;
try {
<span style="white-space:pre"> </span>ret = new StringEntity(jObj.toJSONString(), HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}contentType就是“application/json”
当然,这个entity也会被进一步的处理

后记:

这一篇并没有什么干货啊,后面会陆陆续续再写一点吧,目的就定位为:方便一些同学不看源码也知道一些“坑”,前三篇我会再过一遍、有错误的地方会尽快改掉。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息