1
+ package com .loopj .android .http ;
2
+
3
+ import java .io .IOException ;
4
+ import java .io .InputStream ;
5
+ import java .util .concurrent .ExecutorService ;
6
+ import java .util .concurrent .Executors ;
7
+ import java .util .zip .GZIPInputStream ;
8
+
9
+ import org .apache .http .Header ;
10
+ import org .apache .http .HeaderElement ;
11
+ import org .apache .http .HttpEntity ;
12
+ import org .apache .http .HttpRequest ;
13
+ import org .apache .http .HttpRequestInterceptor ;
14
+ import org .apache .http .HttpResponse ;
15
+ import org .apache .http .HttpResponseInterceptor ;
16
+ import org .apache .http .HttpVersion ;
17
+ import org .apache .http .client .CookieStore ;
18
+ import org .apache .http .client .methods .HttpGet ;
19
+ import org .apache .http .client .methods .HttpPost ;
20
+ import org .apache .http .client .methods .HttpRequestBase ;
21
+ import org .apache .http .client .protocol .ClientContext ;
22
+ import org .apache .http .conn .params .ConnManagerParams ;
23
+ import org .apache .http .conn .params .ConnPerRouteBean ;
24
+ import org .apache .http .conn .scheme .PlainSocketFactory ;
25
+ import org .apache .http .conn .scheme .Scheme ;
26
+ import org .apache .http .conn .scheme .SchemeRegistry ;
27
+ import org .apache .http .conn .ssl .SSLSocketFactory ;
28
+ import org .apache .http .entity .HttpEntityWrapper ;
29
+ import org .apache .http .impl .client .DefaultHttpClient ;
30
+ import org .apache .http .impl .conn .tsccm .ThreadSafeClientConnManager ;
31
+ import org .apache .http .params .BasicHttpParams ;
32
+ import org .apache .http .params .HttpConnectionParams ;
33
+ import org .apache .http .params .HttpProtocolParams ;
34
+ import org .apache .http .protocol .BasicHttpContext ;
35
+ import org .apache .http .protocol .SyncBasicHttpContext ;
36
+ import org .apache .http .protocol .HttpContext ;
37
+
38
+ public class AsyncHttpClient {
39
+ public static final int DEFAULT_MAX_CONNECTIONS = 4 ;
40
+ public static final int DEFAULT_SOCKET_TIMEOUT = 30 * 1000 ;
41
+ private static final String ENCODING = "UTF-8" ;
42
+ private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
43
+ private static final String ENCODING_GZIP = "gzip" ;
44
+
45
+ private static int maxConnections = DEFAULT_MAX_CONNECTIONS ;
46
+ private static int socketTimeout = DEFAULT_SOCKET_TIMEOUT ;
47
+
48
+ private DefaultHttpClient httpClient ;
49
+ private HttpContext httpContext ;
50
+ private ExecutorService threadPool ;
51
+
52
+ public AsyncHttpClient (String userAgent ) {
53
+ BasicHttpParams httpParams = new BasicHttpParams ();
54
+
55
+ ConnManagerParams .setTimeout (httpParams , socketTimeout );
56
+ ConnManagerParams .setMaxConnectionsPerRoute (httpParams , new ConnPerRouteBean (maxConnections ));
57
+ ConnManagerParams .setMaxTotalConnections (httpParams , DEFAULT_MAX_CONNECTIONS );
58
+
59
+ HttpConnectionParams .setSoTimeout (httpParams , socketTimeout );
60
+ HttpConnectionParams .setTcpNoDelay (httpParams , true );
61
+
62
+ HttpProtocolParams .setVersion (httpParams , HttpVersion .HTTP_1_1 );
63
+ HttpProtocolParams .setUserAgent (httpParams , userAgent );
64
+
65
+ SchemeRegistry schemeRegistry = new SchemeRegistry ();
66
+ schemeRegistry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
67
+ schemeRegistry .register (new Scheme ("https" , SSLSocketFactory .getSocketFactory (), 443 ));
68
+ ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager (httpParams , schemeRegistry );
69
+
70
+ threadPool = Executors .newCachedThreadPool ();
71
+ httpContext = new SyncBasicHttpContext (new BasicHttpContext ());
72
+ httpClient = new DefaultHttpClient (cm , httpParams );
73
+ httpClient .addRequestInterceptor (new HttpRequestInterceptor () {
74
+ public void process (HttpRequest request , HttpContext context ) {
75
+ if (!request .containsHeader (HEADER_ACCEPT_ENCODING )) {
76
+ request .addHeader (HEADER_ACCEPT_ENCODING , ENCODING_GZIP );
77
+ }
78
+ }
79
+ });
80
+
81
+ httpClient .addResponseInterceptor (new HttpResponseInterceptor () {
82
+ public void process (HttpResponse response , HttpContext context ) {
83
+ final HttpEntity entity = response .getEntity ();
84
+ final Header encoding = entity .getContentEncoding ();
85
+ if (encoding != null ) {
86
+ for (HeaderElement element : encoding .getElements ()) {
87
+ if (element .getName ().equalsIgnoreCase (ENCODING_GZIP )) {
88
+ response .setEntity (new InflatingEntity (response .getEntity ()));
89
+ break ;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ });
95
+ }
96
+
97
+ public void setCookieStore (CookieStore cookieStore ) {
98
+ httpContext .setAttribute (ClientContext .COOKIE_STORE , cookieStore );
99
+ }
100
+
101
+ public void get (String url , AsyncHttpRequest .OnResponseHandler responseHandler ) {
102
+ get (url , null , responseHandler );
103
+ }
104
+
105
+ public void get (String url , AsyncHttpParams params , AsyncHttpRequest .OnResponseHandler responseHandler ) {
106
+ // Build and append query string (utf8 url encoded)
107
+ if (params != null ) {
108
+ String paramString = params .getParamString ();
109
+ url += "?" + paramString ;
110
+ }
111
+
112
+ // Fire up the request in a new thread
113
+ executeAsyncRequest (new HttpGet (url ), responseHandler );
114
+ }
115
+
116
+ public void post (String url , AsyncHttpRequest .OnResponseHandler responseHandler ) {
117
+ post (url , null , responseHandler );
118
+ }
119
+
120
+ public void post (String url , AsyncHttpParams params , AsyncHttpRequest .OnResponseHandler responseHandler ) {
121
+ // Build post object with params
122
+ final HttpPost post = new HttpPost (url );
123
+ if (params != null ) {
124
+ HttpEntity entity = params .getEntity ();
125
+ if (entity != null ){
126
+ post .setEntity (entity );
127
+ }
128
+ }
129
+
130
+ // Fire up the request in a new thread
131
+ executeAsyncRequest (post , responseHandler );
132
+ }
133
+
134
+ private void executeAsyncRequest (final HttpRequestBase request , final AsyncHttpRequest .OnResponseHandler responseHandler ) {
135
+ threadPool .execute (new AsyncHttpRequest (httpClient , httpContext , request , responseHandler ));
136
+ }
137
+
138
+ private static class InflatingEntity extends HttpEntityWrapper {
139
+ public InflatingEntity (HttpEntity wrapped ) {
140
+ super (wrapped );
141
+ }
142
+
143
+ @ Override
144
+ public InputStream getContent () throws IOException {
145
+ return new GZIPInputStream (wrappedEntity .getContent ());
146
+ }
147
+
148
+ @ Override
149
+ public long getContentLength () {
150
+ return -1 ;
151
+ }
152
+ }
153
+ }
0 commit comments