Mad Aces v1.2.2.3版本的 MD5 值为:f29ebd7c5808053d9f3f3722a5db1688
以下内容为反编译后的 AsyncHttpClient.java 源代码,内容仅作参考
package com.heyzap.http;
import android.content.Context;
import com.heyzap.common.concurrency.ExecutorPool;
import com.heyzap.internal.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRedirectHandler;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.SyncBasicHttpContext;
public class AsyncHttpClient {
public static final int DEFAULT_MAX_CONNECTIONS = 10;
public static final int DEFAULT_MAX_RETRIES = 0;
public static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500;
public static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
public static final int DEFAULT_SOCKET_TIMEOUT = 10000;
public static final String ENCODING_GZIP = "gzip";
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
public static final String LOG_TAG = "AsyncHttpClient";
public static final String VERSION = "1.4.5";
private final Map<String, String> clientHeaderMap;
private final DefaultHttpClient httpClient;
private final HttpContext httpContext;
private boolean isUrlEncodingEnabled;
private int maxConnections;
private final Object requestLock;
private final Map<Context, List<RequestHandle>> requestMap;
private ExecutorService threadPool;
private int timeout;
public AsyncHttpClient() {
this(false, 80, 443);
}
public AsyncHttpClient(int i) {
this(false, i, 443);
}
public AsyncHttpClient(int i, int i2) {
this(false, i, i2);
}
public AsyncHttpClient(boolean z, int i, int i2) {
this(getDefaultSchemeRegistry(z, i, i2));
}
private static SchemeRegistry getDefaultSchemeRegistry(boolean z, int i, int i2) {
SSLSocketFactory socketFactory;
if (z) {
}
if (i < 1) {
i = 80;
}
if (i2 < 1) {
i2 = 443;
}
if (z) {
socketFactory = MySSLSocketFactory.getFixedSocketFactory();
} else {
socketFactory = SSLSocketFactory.getSocketFactory();
}
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), i));
schemeRegistry.register(new Scheme("https", socketFactory, i2));
return schemeRegistry;
}
public AsyncHttpClient(SchemeRegistry schemeRegistry) {
this.maxConnections = 10;
this.timeout = 10000;
this.isUrlEncodingEnabled = true;
this.requestLock = new Object();
BasicHttpParams basicHttpParams = new BasicHttpParams();
ConnManagerParams.setTimeout(basicHttpParams, this.timeout);
ConnManagerParams.setMaxConnectionsPerRoute(basicHttpParams, new ConnPerRouteBean(this.maxConnections));
ConnManagerParams.setMaxTotalConnections(basicHttpParams, 10);
HttpConnectionParams.setSoTimeout(basicHttpParams, this.timeout);
HttpConnectionParams.setConnectionTimeout(basicHttpParams, this.timeout);
HttpConnectionParams.setTcpNoDelay(basicHttpParams, true);
HttpConnectionParams.setSocketBufferSize(basicHttpParams, 8192);
HttpProtocolParams.setVersion(basicHttpParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUserAgent(basicHttpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION));
ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager(basicHttpParams, schemeRegistry);
this.threadPool = ExecutorPool.getInstance();
this.requestMap = new WeakHashMap();
this.clientHeaderMap = new HashMap();
this.httpContext = new SyncBasicHttpContext(new BasicHttpContext());
this.httpClient = new DefaultHttpClient(threadSafeClientConnManager, basicHttpParams);
this.httpClient.addRequestInterceptor(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) {
if (!httpRequest.containsHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING)) {
httpRequest.addHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING, AsyncHttpClient.ENCODING_GZIP);
}
for (String str : AsyncHttpClient.this.clientHeaderMap.keySet()) {
httpRequest.addHeader(str, (String) AsyncHttpClient.this.clientHeaderMap.get(str));
}
}
});
this.httpClient.addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse httpResponse, HttpContext httpContext) {
Header contentEncoding;
HttpEntity entity = httpResponse.getEntity();
if (entity != null && (contentEncoding = entity.getContentEncoding()) != null) {
HeaderElement[] elements = contentEncoding.getElements();
for (HeaderElement headerElement : elements) {
if (headerElement.getName().equalsIgnoreCase(AsyncHttpClient.ENCODING_GZIP)) {
httpResponse.setEntity(new InflatingEntity(entity));
return;
}
}
}
}
});
this.httpClient.setHttpRequestRetryHandler(new RetryHandler(0, 1500));
}
public static void allowRetryExceptionClass(Class<?> cls) {
if (cls != null) {
RetryHandler.addClassToWhitelist(cls);
}
}
public static void blockRetryExceptionClass(Class<?> cls) {
if (cls != null) {
RetryHandler.addClassToBlacklist(cls);
}
}
public DefaultHttpClient getHttpClient() {
return this.httpClient;
}
public HttpContext getHttpContext() {
return this.httpContext;
}
public void setCookieStore(CookieStore cookieStore) {
this.httpContext.setAttribute("http.cookie-store", cookieStore);
}
public void setThreadPool(ExecutorService executorService) {
this.threadPool = executorService;
}
public void setEnableRedirects(final boolean z) {
this.httpClient.setRedirectHandler(new DefaultRedirectHandler() {
@Override
public boolean isRedirectRequested(HttpResponse httpResponse, HttpContext httpContext) {
return z;
}
});
}
public void setUserAgent(String str) {
HttpProtocolParams.setUserAgent(this.httpClient.getParams(), str);
}
public int getMaxConnections() {
return this.maxConnections;
}
public void setMaxConnections(int i) {
if (i < 1) {
i = 10;
}
this.maxConnections = i;
ConnManagerParams.setMaxConnectionsPerRoute(this.httpClient.getParams(), new ConnPerRouteBean(this.maxConnections));
}
public int getTimeout() {
return this.timeout;
}
public void setTimeout(int i) {
if (i < 1000) {
i = 10000;
}
this.timeout = i;
HttpParams params = this.httpClient.getParams();
ConnManagerParams.setTimeout(params, this.timeout);
HttpConnectionParams.setSoTimeout(params, this.timeout);
HttpConnectionParams.setConnectionTimeout(params, this.timeout);
}
public void setProxy(String str, int i) {
this.httpClient.getParams().setParameter("http.route.default-proxy", new HttpHost(str, i));
}
public void setProxy(String str, int i, String str2, String str3) {
this.httpClient.getCredentialsProvider().setCredentials(new AuthScope(str, i), new UsernamePasswordCredentials(str2, str3));
this.httpClient.getParams().setParameter("http.route.default-proxy", new HttpHost(str, i));
}
public void setSSLSocketFactory(SSLSocketFactory sSLSocketFactory) {
this.httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sSLSocketFactory, 443));
}
public void setMaxRetriesAndTimeout(int i, int i2) {
this.httpClient.setHttpRequestRetryHandler(new RetryHandler(i, i2));
}
public void addHeader(String str, String str2) {
this.clientHeaderMap.put(str, str2);
}
public void removeHeader(String str) {
this.clientHeaderMap.remove(str);
}
public void setBasicAuth(String str, String str2) {
setBasicAuth(str, str2, AuthScope.ANY);
}
public void setBasicAuth(String str, String str2, AuthScope authScope) {
this.httpClient.getCredentialsProvider().setCredentials(authScope, new UsernamePasswordCredentials(str, str2));
}
public void clearBasicAuth() {
this.httpClient.getCredentialsProvider().clear();
}
public void cancelRequests(Context context, boolean z) {
List<RequestHandle> list = this.requestMap.get(context);
if (list != null) {
Iterator<RequestHandle> it = list.iterator();
while (it.hasNext()) {
it.next().cancel(z);
}
this.requestMap.remove(context);
}
}
public RequestHandle head(String str, ResponseHandlerInterface responseHandlerInterface) {
return head(null, str, null, responseHandlerInterface);
}
public RequestHandle head(String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return head(null, str, requestParams, responseHandlerInterface);
}
public RequestHandle head(Context context, String str, ResponseHandlerInterface responseHandlerInterface) {
return head(context, str, null, responseHandlerInterface);
}
public RequestHandle head(Context context, String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return sendRequest(this.httpClient, this.httpContext, new HttpHead(getUrlWithQueryString(this.isUrlEncodingEnabled, str, requestParams)), null, responseHandlerInterface, context);
}
public RequestHandle head(Context context, String str, Header[] headerArr, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
HttpHead httpHead = new HttpHead(getUrlWithQueryString(this.isUrlEncodingEnabled, str, requestParams));
if (headerArr != null) {
httpHead.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, httpHead, null, responseHandlerInterface, context);
}
public RequestHandle get(String str, ResponseHandlerInterface responseHandlerInterface) {
return get(null, str, null, responseHandlerInterface);
}
public RequestHandle get(String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return get(null, str, requestParams, responseHandlerInterface);
}
public RequestHandle get(Context context, String str, ResponseHandlerInterface responseHandlerInterface) {
return get(context, str, null, responseHandlerInterface);
}
public RequestHandle get(Context context, String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return sendRequest(this.httpClient, this.httpContext, new HttpGet(getUrlWithQueryString(this.isUrlEncodingEnabled, str, requestParams)), null, responseHandlerInterface, context);
}
public RequestHandle get(Context context, String str, Header[] headerArr, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
HttpGet httpGet = new HttpGet(getUrlWithQueryString(this.isUrlEncodingEnabled, str, requestParams));
if (headerArr != null) {
httpGet.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, httpGet, null, responseHandlerInterface, context);
}
public RequestHandle post(String str, ResponseHandlerInterface responseHandlerInterface) {
return post(null, str, null, responseHandlerInterface);
}
public RequestHandle post(String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return post(null, str, requestParams, responseHandlerInterface);
}
public RequestHandle post(Context context, String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return post(context, str, paramsToEntity(requestParams, responseHandlerInterface), null, responseHandlerInterface);
}
public RequestHandle post(Context context, String str, HttpEntity httpEntity, String str2, ResponseHandlerInterface responseHandlerInterface) {
return sendRequest(this.httpClient, this.httpContext, addEntityToRequestBase(new HttpPost(str), httpEntity), str2, responseHandlerInterface, context);
}
public RequestHandle post(Context context, String str, Header[] headerArr, RequestParams requestParams, String str2, ResponseHandlerInterface responseHandlerInterface) {
HttpPost httpPost = new HttpPost(str);
if (requestParams != null) {
httpPost.setEntity(paramsToEntity(requestParams, responseHandlerInterface));
}
if (headerArr != null) {
httpPost.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, httpPost, str2, responseHandlerInterface, context);
}
public RequestHandle post(Context context, String str, Header[] headerArr, HttpEntity httpEntity, String str2, ResponseHandlerInterface responseHandlerInterface) {
HttpEntityEnclosingRequestBase addEntityToRequestBase = addEntityToRequestBase(new HttpPost(str), httpEntity);
if (headerArr != null) {
addEntityToRequestBase.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, addEntityToRequestBase, str2, responseHandlerInterface, context);
}
public RequestHandle put(String str, ResponseHandlerInterface responseHandlerInterface) {
return put(null, str, null, responseHandlerInterface);
}
public RequestHandle put(String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return put(null, str, requestParams, responseHandlerInterface);
}
public RequestHandle put(Context context, String str, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
return put(context, str, paramsToEntity(requestParams, responseHandlerInterface), null, responseHandlerInterface);
}
public RequestHandle put(Context context, String str, HttpEntity httpEntity, String str2, ResponseHandlerInterface responseHandlerInterface) {
return sendRequest(this.httpClient, this.httpContext, addEntityToRequestBase(new HttpPut(str), httpEntity), str2, responseHandlerInterface, context);
}
public RequestHandle put(Context context, String str, Header[] headerArr, HttpEntity httpEntity, String str2, ResponseHandlerInterface responseHandlerInterface) {
HttpEntityEnclosingRequestBase addEntityToRequestBase = addEntityToRequestBase(new HttpPut(str), httpEntity);
if (headerArr != null) {
addEntityToRequestBase.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, addEntityToRequestBase, str2, responseHandlerInterface, context);
}
public RequestHandle delete(String str, ResponseHandlerInterface responseHandlerInterface) {
return delete(null, str, responseHandlerInterface);
}
public RequestHandle delete(Context context, String str, ResponseHandlerInterface responseHandlerInterface) {
return sendRequest(this.httpClient, this.httpContext, new HttpDelete(str), null, responseHandlerInterface, context);
}
public RequestHandle delete(Context context, String str, Header[] headerArr, ResponseHandlerInterface responseHandlerInterface) {
HttpDelete httpDelete = new HttpDelete(str);
if (headerArr != null) {
httpDelete.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, httpDelete, null, responseHandlerInterface, context);
}
public RequestHandle delete(Context context, String str, Header[] headerArr, RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
HttpDelete httpDelete = new HttpDelete(getUrlWithQueryString(this.isUrlEncodingEnabled, str, requestParams));
if (headerArr != null) {
httpDelete.setHeaders(headerArr);
}
return sendRequest(this.httpClient, this.httpContext, httpDelete, null, responseHandlerInterface, context);
}
protected RequestHandle sendRequest(DefaultHttpClient defaultHttpClient, HttpContext httpContext, HttpUriRequest httpUriRequest, String str, ResponseHandlerInterface responseHandlerInterface, Context context) {
if (str != null) {
httpUriRequest.setHeader("Content-Type", str);
}
responseHandlerInterface.setRequestHeaders(httpUriRequest.getAllHeaders());
responseHandlerInterface.setRequestURI(httpUriRequest.getURI());
AsyncHttpRequest asyncHttpRequest = new AsyncHttpRequest(defaultHttpClient, httpContext, httpUriRequest, responseHandlerInterface);
this.threadPool.submit(asyncHttpRequest);
RequestHandle requestHandle = new RequestHandle(asyncHttpRequest);
if (context != null) {
List<RequestHandle> list = this.requestMap.get(context);
if (list == null) {
list = new LinkedList<>();
this.requestMap.put(context, list);
}
synchronized (this.requestLock) {
list.add(requestHandle);
Iterator<RequestHandle> it = list.iterator();
while (it.hasNext()) {
if (it.next().shouldBeGarbageCollected()) {
it.remove();
}
}
}
}
return requestHandle;
}
public void setURLEncodingEnabled(boolean z) {
this.isUrlEncodingEnabled = z;
}
public static String getUrlWithQueryString(boolean z, String str, RequestParams requestParams) {
String replace = z ? str.replace(" ", "%20") : str;
if (requestParams != null) {
String paramString = requestParams.getParamString();
if (!replace.contains("?")) {
return replace + "?" + paramString;
}
return replace + "&" + paramString;
}
return replace;
}
private HttpEntity paramsToEntity(RequestParams requestParams, ResponseHandlerInterface responseHandlerInterface) {
if (requestParams == null) {
return null;
}
try {
return requestParams.getEntity(responseHandlerInterface);
} catch (Throwable th) {
if (responseHandlerInterface != null) {
responseHandlerInterface.sendFailureMessage(0, null, null, th);
return null;
}
Logger.trace(th);
return null;
}
}
public boolean isUrlEncodingEnabled() {
return this.isUrlEncodingEnabled;
}
private HttpEntityEnclosingRequestBase addEntityToRequestBase(HttpEntityEnclosingRequestBase httpEntityEnclosingRequestBase, HttpEntity httpEntity) {
if (httpEntity != null) {
httpEntityEnclosingRequestBase.setEntity(httpEntity);
}
return httpEntityEnclosingRequestBase;
}
private static class InflatingEntity extends HttpEntityWrapper {
private GZIPInputStream stream;
public InflatingEntity(HttpEntity httpEntity) {
super(httpEntity);
}
@Override
public InputStream getContent() throws IOException {
this.stream = new GZIPInputStream(this.wrappedEntity.getContent());
return this.stream;
}
@Override
public long getContentLength() {
return -1L;
}
public void finalize() throws Throwable {
super.finalize();
if (this.stream != null) {
try {
this.stream.close();
} catch (Exception e) {
}
}
}
}
}