39
39
import org .apache .http .HttpResponseInterceptor ;
40
40
import org .apache .http .HttpVersion ;
41
41
import org .apache .http .client .CookieStore ;
42
+ import org .apache .http .client .HttpClient ;
42
43
import org .apache .http .client .methods .HttpGet ;
43
44
import org .apache .http .client .methods .HttpPost ;
44
45
import org .apache .http .client .methods .HttpPut ;
76
77
* For example:
77
78
* <p>
78
79
* <pre>
79
- * AsyncHttpClient client = new AsyncHttpClient("My User Agent" );
80
+ * AsyncHttpClient client = new AsyncHttpClient();
80
81
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
81
82
* @Override
82
83
* public void onSuccess(String response) {
86
87
* </pre>
87
88
*/
88
89
public class AsyncHttpClient {
90
+ private static final String VERSION = "1.3.1" ;
91
+
89
92
private static final int DEFAULT_MAX_CONNECTIONS = 10 ;
90
93
private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000 ;
91
94
private static final int DEFAULT_MAX_RETRIES = 5 ;
92
- private static final String ENCODING = "UTF-8" ;
95
+ private static final String ENCODING = "UTF-8" ;
93
96
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding" ;
94
97
private static final String ENCODING_GZIP = "gzip" ;
95
98
@@ -101,11 +104,11 @@ public class AsyncHttpClient {
101
104
private ThreadPoolExecutor threadPool ;
102
105
private Map <Context , List <WeakReference <Future >>> requestMap ;
103
106
107
+
104
108
/**
105
- * Creates a new AsyncHttpClient which will identify itself with the user agent userAgent
106
- * @param userAgent the identifier to use in the User-Agent header in requests
109
+ * Creates a new AsyncHttpClient.
107
110
*/
108
- public AsyncHttpClient (String userAgent ) {
111
+ public AsyncHttpClient () {
109
112
BasicHttpParams httpParams = new BasicHttpParams ();
110
113
111
114
ConnManagerParams .setTimeout (httpParams , socketTimeout );
@@ -116,7 +119,7 @@ public AsyncHttpClient(String userAgent) {
116
119
HttpConnectionParams .setTcpNoDelay (httpParams , true );
117
120
118
121
HttpProtocolParams .setVersion (httpParams , HttpVersion .HTTP_1_1 );
119
- HttpProtocolParams .setUserAgent (httpParams , userAgent );
122
+ HttpProtocolParams .setUserAgent (httpParams , String . format ( "android-async-http/%s (http://loopj.com/android-async-http)" , VERSION ) );
120
123
121
124
SchemeRegistry schemeRegistry = new SchemeRegistry ();
122
125
schemeRegistry .register (new Scheme ("http" , PlainSocketFactory .getSocketFactory (), 80 ));
@@ -155,6 +158,15 @@ public void process(HttpResponse response, HttpContext context) {
155
158
requestMap = new WeakHashMap <Context , List <WeakReference <Future >>>();
156
159
}
157
160
161
+ /**
162
+ * Get the underlying HttpClient instance. This is useful for setting
163
+ * additional fine-grained settings for requests by accessing the
164
+ * client's ConnectionManager, HttpParams and SchemeRegistry.
165
+ */
166
+ public HttpClient getHttpClient () {
167
+ return this .httpClient ;
168
+ }
169
+
158
170
/**
159
171
* Sets an optional CookieStore to use when making requests
160
172
* @param cookieStore The CookieStore implementation to use, usually an instance of {@link PersistentCookieStore}
@@ -172,6 +184,24 @@ public void setThreadPool(ThreadPoolExecutor threadPool) {
172
184
this .threadPool = threadPool ;
173
185
}
174
186
187
+ /**
188
+ * Sets the User-Agent header to be sent with each request. By default,
189
+ * "Android Asynchronous Http Client/VERSION (http://loopj.com/android-async-http/)" is used.
190
+ * @param userAgent the string to use in the User-Agent header.
191
+ */
192
+ public void setUserAgent (String userAgent ) {
193
+ HttpProtocolParams .setUserAgent (this .httpClient .getParams (), userAgent );
194
+ }
195
+
196
+ /**
197
+ * Sets the SSLSocketFactory to user when making requests. By default,
198
+ * a new, default SSLSocketFactory is used.
199
+ * @param sslSocketFactory the socket factory to use for https requests.
200
+ */
201
+ public void setSSLSocketFactory (SSLSocketFactory sslSocketFactory ) {
202
+ this .httpClient .getConnectionManager ().getSchemeRegistry ().register (new Scheme ("https" , sslSocketFactory , 443 ));
203
+ }
204
+
175
205
/**
176
206
* Cancels any pending (or potentially active) requests associated with the
177
207
* passed Context.
0 commit comments