Skip to content

Commit 177281d

Browse files
committedJun 4, 2011
Update constructor, add in more underlying http client control and bump version
1 parent 8db120b commit 177281d

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed
 

‎build.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- Package properties -->
66
<property name="package.name" value="android-async-http" />
7-
<property name="package.version" value="1.2.1" />
7+
<property name="package.version" value="1.3.1" />
88
<property name="package.packagename" value="com.loopj.android.http" />
99

1010
<!-- Standard jar stuff -->
@@ -35,7 +35,7 @@
3535
<target name="compile">
3636
<mkdir dir="${build.dir}" />
3737
<mkdir dir="${classes.dir}" />
38-
<javac srcdir="." destdir="${classes.dir}" classpathref="classpath" />
38+
<javac srcdir="src" destdir="${classes.dir}" classpathref="classpath" />
3939
</target>
4040

4141
<!-- Package a jar from compiled class files -->

‎examples/ExampleUsage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class ExampleUsage {
44
public static void makeRequest() {
5-
AsyncHttpClient client = new AsyncHttpClient("My User Agent");
5+
AsyncHttpClient client = new AsyncHttpClient();
66

77
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
88
@Override

‎examples/TwitterRestClient.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import com.loopj.android.http.*;
44

55
public class TwitterRestClient {
6-
private static final String USER_AGENT = "Example Twitter Rest Client";
76
private static final String BASE_URL = "http://api.twitter.com/1/";
87

9-
private static AsyncHttpClient client = new AsyncHttpClient(USER_AGENT);
8+
private static AsyncHttpClient client = new AsyncHttpClient();
109

1110
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
1211
client.get(getAbsoluteUrl(url), params, responseHandler);

‎src/com/loopj/android/http/AsyncHttpClient.java

+36-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.http.HttpResponseInterceptor;
4040
import org.apache.http.HttpVersion;
4141
import org.apache.http.client.CookieStore;
42+
import org.apache.http.client.HttpClient;
4243
import org.apache.http.client.methods.HttpGet;
4344
import org.apache.http.client.methods.HttpPost;
4445
import org.apache.http.client.methods.HttpPut;
@@ -76,7 +77,7 @@
7677
* For example:
7778
* <p>
7879
* <pre>
79-
* AsyncHttpClient client = new AsyncHttpClient("My User Agent");
80+
* AsyncHttpClient client = new AsyncHttpClient();
8081
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
8182
* &#064;Override
8283
* public void onSuccess(String response) {
@@ -86,10 +87,12 @@
8687
* </pre>
8788
*/
8889
public class AsyncHttpClient {
90+
private static final String VERSION = "1.3.1";
91+
8992
private static final int DEFAULT_MAX_CONNECTIONS = 10;
9093
private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
9194
private static final int DEFAULT_MAX_RETRIES = 5;
92-
private static final String ENCODING = "UTF-8";
95+
private static final String ENCODING = "UTF-8";
9396
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
9497
private static final String ENCODING_GZIP = "gzip";
9598

@@ -101,11 +104,11 @@ public class AsyncHttpClient {
101104
private ThreadPoolExecutor threadPool;
102105
private Map<Context, List<WeakReference<Future>>> requestMap;
103106

107+
104108
/**
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.
107110
*/
108-
public AsyncHttpClient(String userAgent) {
111+
public AsyncHttpClient() {
109112
BasicHttpParams httpParams = new BasicHttpParams();
110113

111114
ConnManagerParams.setTimeout(httpParams, socketTimeout);
@@ -116,7 +119,7 @@ public AsyncHttpClient(String userAgent) {
116119
HttpConnectionParams.setTcpNoDelay(httpParams, true);
117120

118121
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));
120123

121124
SchemeRegistry schemeRegistry = new SchemeRegistry();
122125
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
@@ -155,6 +158,15 @@ public void process(HttpResponse response, HttpContext context) {
155158
requestMap = new WeakHashMap<Context, List<WeakReference<Future>>>();
156159
}
157160

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+
158170
/**
159171
* Sets an optional CookieStore to use when making requests
160172
* @param cookieStore The CookieStore implementation to use, usually an instance of {@link PersistentCookieStore}
@@ -172,6 +184,24 @@ public void setThreadPool(ThreadPoolExecutor threadPool) {
172184
this.threadPool = threadPool;
173185
}
174186

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+
175205
/**
176206
* Cancels any pending (or potentially active) requests associated with the
177207
* passed Context.

‎src/com/loopj/android/http/AsyncHttpResponseHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* For example:
4343
* <p>
4444
* <pre>
45-
* AsyncHttpClient client = new AsyncHttpClient("My User Agent");
45+
* AsyncHttpClient client = new AsyncHttpClient();
4646
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
4747
* &#064;Override
4848
* public void onStart() {

‎src/com/loopj/android/http/RequestParams.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* params.put("profile_picture2", someInputStream); // Upload an InputStream
4949
* params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes
5050
*
51-
* AsyncHttpClient client = new AsyncHttpClient("My User Agent");
51+
* AsyncHttpClient client = new AsyncHttpClient();
5252
* client.post("http://myendpoint.com", params, responseHandler);
5353
* </pre>
5454
*/

0 commit comments

Comments
 (0)