android-async-http源码分析
首先查看官方文档的一个例子:
AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", new AsyncHttpResponseHandler() { @Override public void onStart() { // called before request is started } @Override public void onSuccess(int statusCode, Header[] headers, byte[] response) { // called when response HTTP status is "200 OK" } @Override public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) { // called when response HTTP status is "4XX" (eg. 401, 403, 404) } @Override public void onRetry(int retryNo) { // called when request is retried } });
找到源码目录中的 AsyncHttpClient.java
public RequestHandle get(String url, ResponseHandlerInterface responseHandler) { return get(null, url, null, responseHandler); } public RequestHandle get(Context context, String url, RequestParams params, ResponseHandlerInterface responseHandler) { return sendRequest(httpClient, httpContext, new HttpGet(getUrlWithQueryString(isUrlEncodingEnabled, url, params)), null, responseHandler, context); }
先来看看这个函数 getUrlWithQueryString,这个函数主要是转换地址,还有将get参数并接起来的,比如
http://my.oschina.net/hailongqiu/admin/edit-blog?blog=390660
blog = 390660 这个就是参数. RequestParams params 是通过这个存储的.不过我们传入的是null.
public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) { if (url == null) return null; if (shouldEncodeUrl) { try { String decodedURL = URLDecoder.decode(url, "UTF-8"); URL _url = new URL(decodedURL); URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(), _url.getPath(), _url.getQuery(), _url.getRef()); url = _uri.toASCIIString(); } catch (Exception ex) { // Should not really happen, added just for sake of validity Log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex); } } if (params != null) { // Construct the query string and trim it, in case it // includes any excessive white spaces. String paramString = params.getParamString().trim(); // Only add the query string if it isn‘t empty and it // isn‘t equal to ‘?‘. if (!paramString.equals("") && !paramString.equals("?")) { url += url.contains("?") ? "&" : "?"; url += paramString; } } return url; }
ff
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。