安卓阵营在Android M版本就放弃org.apache.http.legacy,对于构建在其上面的android-async-http,github也不再更新。虽然其已经退出了历史舞台,但对我这样的仅仅知道调用、调用的码农,尚未知道这个框架是怎么运行的。最近花费时间研究一下,期望可以对自己的分析源码、软件设计有些提升。肯定有多不做之处,期待您的批评。
asyncHttpClient.post(MainActivity.this.getApplicationContext(), path, stringEntity, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
tv.setText(new String(responseBody));
}
@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
tv.setText(new String(responseBody));
}
});
点击post方法进行一步步跟踪到sendRequest
AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context);
threadPool.submit(request);
也就是说具体的网络请求是通过在线程池中执行Runnable——AsyncHttpRequest ,完成的。
AsyncHttpRequest.run() -> makeRequestWithRetries() -> makeRequest()
private void makeRequest() throws IOException {
...
HttpResponse response = client.execute(request, context);//进行具体的网络请求
...
responseHandler.sendResponseMessage(response);
}
网络请求结果是通过responseHandler处理的,而它恰恰就是我们post传递的参数AsyncHttpResponseHandler。
AsyncHttpResponseHandler.sendResponseMessage
@Override
public void sendResponseMessage(HttpResponse response) throws IOException {
//解析HttpResponse.
...
sendFailureMessage(status.getStatusCode(), response.getAllHeaders(), responseBody, new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()));
} else {
sendSuccessMessage(status.getStatusCode(), response.getAllHeaders(), responseBody);
}
}
}
}
@Override
final public void sendSuccessMessage(int statusCode, Header[] headers, byte[] responseBytes) {
sendMessage(obtainMessage(SUCCESS_MESSAGE, new Object[]{statusCode, headers, responseBytes}));
}
@Override
final public void sendFailureMessage(int statusCode, Header[] headers, byte[] responseBody, Throwable throwable) {
sendMessage(obtainMessage(FAILURE_MESSAGE, new Object[]{statusCode, headers, responseBody, throwable}));
}
protected Message obtainMessage(int responseMessageId, Object responseMessageData) {
return Message.obtain(handler, responseMessageId, responseMessageData);
}
这里生成的Message就是Android应用开发中常见的Message了。
protected void sendMessage(Message msg) {
handler.sendMessage(msg);
}
}
这里的handler就是Android应用开发中常见的Handler了。
下面肯定要分析handleMessage方法咯
/**
* Avoid leaks by using a non-anonymous handler class.
*/
private static class ResponderHandler extends Handler {
private final AsyncHttpResponseHandler mResponder;
ResponderHandler(AsyncHttpResponseHandler mResponder, Looper looper) {
super(looper);
this.mResponder = mResponder;
}
@Override
public void handleMessage(Message msg) {
mResponder.handleMessage(msg);
}
}
这里用到了代理模式,结果又调用了AsyncHttpResponseHandler.handleMessage()
// Methods which emulate android's Handler and Message methods
protected void handleMessage(Message message) {
...
switch (message.what) {
case SUCCESS_MESSAGE:
...
onSuccess((Integer) response[0], (Header[]) response[1], (byte[]) response[2]);
...
break;
case FAILURE_MESSAGE:
...
onFailure((Integer) response[0], (Header[]) response[1], (byte[]) response[2], (Throwable) response[3]);
...
break;
...
}
}
这里onSuccess && onFailure 就是我们传入post中AsyncHttpResponseHandler对象的方法了。