From 177281d806b03fc22f440c190a4c8424d962c732 Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 4 Jun 2011 15:53:53 -0700 Subject: [PATCH] Update constructor, add in more underlying http client control and bump version --- build.xml | 4 +- examples/ExampleUsage.java | 2 +- examples/TwitterRestClient.java | 3 +- .../loopj/android/http/AsyncHttpClient.java | 42 ++++++++++++++++--- .../http/AsyncHttpResponseHandler.java | 2 +- src/com/loopj/android/http/RequestParams.java | 2 +- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/build.xml b/build.xml index feedf783c..c5a1cce8e 100644 --- a/build.xml +++ b/build.xml @@ -4,7 +4,7 @@ - + @@ -35,7 +35,7 @@ - + diff --git a/examples/ExampleUsage.java b/examples/ExampleUsage.java index b6ad1d1cf..2b7a4fa0a 100644 --- a/examples/ExampleUsage.java +++ b/examples/ExampleUsage.java @@ -2,7 +2,7 @@ public class ExampleUsage { public static void makeRequest() { - AsyncHttpClient client = new AsyncHttpClient("My User Agent"); + AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", new AsyncHttpResponseHandler() { @Override diff --git a/examples/TwitterRestClient.java b/examples/TwitterRestClient.java index 05a776e96..f55708cf3 100644 --- a/examples/TwitterRestClient.java +++ b/examples/TwitterRestClient.java @@ -3,10 +3,9 @@ import com.loopj.android.http.*; public class TwitterRestClient { - private static final String USER_AGENT = "Example Twitter Rest Client"; private static final String BASE_URL = "http://api.twitter.com/1/"; - private static AsyncHttpClient client = new AsyncHttpClient(USER_AGENT); + private static AsyncHttpClient client = new AsyncHttpClient(); public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { client.get(getAbsoluteUrl(url), params, responseHandler); diff --git a/src/com/loopj/android/http/AsyncHttpClient.java b/src/com/loopj/android/http/AsyncHttpClient.java index cb8361dc9..6c661ebdc 100644 --- a/src/com/loopj/android/http/AsyncHttpClient.java +++ b/src/com/loopj/android/http/AsyncHttpClient.java @@ -39,6 +39,7 @@ import org.apache.http.HttpResponseInterceptor; import org.apache.http.HttpVersion; import org.apache.http.client.CookieStore; +import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; @@ -76,7 +77,7 @@ * For example: *

*

- * AsyncHttpClient client = new AsyncHttpClient("My User Agent");
+ * AsyncHttpClient client = new AsyncHttpClient();
  * client.get("http://www.google.com", new AsyncHttpResponseHandler() {
  *     @Override
  *     public void onSuccess(String response) {
@@ -86,10 +87,12 @@
  * 
*/ public class AsyncHttpClient { + private static final String VERSION = "1.3.1"; + private static final int DEFAULT_MAX_CONNECTIONS = 10; private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000; private static final int DEFAULT_MAX_RETRIES = 5; - private static final String ENCODING = "UTF-8"; + private static final String ENCODING = "UTF-8"; private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding"; private static final String ENCODING_GZIP = "gzip"; @@ -101,11 +104,11 @@ public class AsyncHttpClient { private ThreadPoolExecutor threadPool; private Map>> requestMap; + /** - * Creates a new AsyncHttpClient which will identify itself with the user agent userAgent - * @param userAgent the identifier to use in the User-Agent header in requests + * Creates a new AsyncHttpClient. */ - public AsyncHttpClient(String userAgent) { + public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); @@ -116,7 +119,7 @@ public AsyncHttpClient(String userAgent) { HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); - HttpProtocolParams.setUserAgent(httpParams, userAgent); + HttpProtocolParams.setUserAgent(httpParams, String.format("android-async-http/%s (http://loopj.com/android-async-http)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); @@ -155,6 +158,15 @@ public void process(HttpResponse response, HttpContext context) { requestMap = new WeakHashMap>>(); } + /** + * Get the underlying HttpClient instance. This is useful for setting + * additional fine-grained settings for requests by accessing the + * client's ConnectionManager, HttpParams and SchemeRegistry. + */ + public HttpClient getHttpClient() { + return this.httpClient; + } + /** * Sets an optional CookieStore to use when making requests * @param cookieStore The CookieStore implementation to use, usually an instance of {@link PersistentCookieStore} @@ -172,6 +184,24 @@ public void setThreadPool(ThreadPoolExecutor threadPool) { this.threadPool = threadPool; } + /** + * Sets the User-Agent header to be sent with each request. By default, + * "Android Asynchronous Http Client/VERSION (http://loopj.com/android-async-http/)" is used. + * @param userAgent the string to use in the User-Agent header. + */ + public void setUserAgent(String userAgent) { + HttpProtocolParams.setUserAgent(this.httpClient.getParams(), userAgent); + } + + /** + * Sets the SSLSocketFactory to user when making requests. By default, + * a new, default SSLSocketFactory is used. + * @param sslSocketFactory the socket factory to use for https requests. + */ + public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) { + this.httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sslSocketFactory, 443)); + } + /** * Cancels any pending (or potentially active) requests associated with the * passed Context. diff --git a/src/com/loopj/android/http/AsyncHttpResponseHandler.java b/src/com/loopj/android/http/AsyncHttpResponseHandler.java index b3274fed0..b17353063 100644 --- a/src/com/loopj/android/http/AsyncHttpResponseHandler.java +++ b/src/com/loopj/android/http/AsyncHttpResponseHandler.java @@ -42,7 +42,7 @@ * For example: *

*

- * AsyncHttpClient client = new AsyncHttpClient("My User Agent");
+ * AsyncHttpClient client = new AsyncHttpClient();
  * client.get("http://www.google.com", new AsyncHttpResponseHandler() {
  *     @Override
  *     public void onStart() {
diff --git a/src/com/loopj/android/http/RequestParams.java b/src/com/loopj/android/http/RequestParams.java
index 76a94fa96..bb656f08a 100644
--- a/src/com/loopj/android/http/RequestParams.java
+++ b/src/com/loopj/android/http/RequestParams.java
@@ -48,7 +48,7 @@
  * params.put("profile_picture2", someInputStream); // Upload an InputStream
  * params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes
  *
- * AsyncHttpClient client = new AsyncHttpClient("My User Agent");
+ * AsyncHttpClient client = new AsyncHttpClient();
  * client.post("http://myendpoint.com", params, responseHandler);
  * 
*/