Skip to content

Commit

Permalink
Update constructor, add in more underlying http client control and bu…
Browse files Browse the repository at this point in the history
…mp version
  • Loading branch information
loopj committed Jun 4, 2011
1 parent 8db120b commit 177281d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<!-- Package properties -->
<property name="package.name" value="android-async-http" />
<property name="package.version" value="1.2.1" />
<property name="package.version" value="1.3.1" />
<property name="package.packagename" value="com.loopj.android.http" />

<!-- Standard jar stuff -->
Expand Down Expand Up @@ -35,7 +35,7 @@
<target name="compile">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<javac srcdir="." destdir="${classes.dir}" classpathref="classpath" />
<javac srcdir="src" destdir="${classes.dir}" classpathref="classpath" />
</target>

<!-- Package a jar from compiled class files -->
Expand Down
2 changes: 1 addition & 1 deletion examples/ExampleUsage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions examples/TwitterRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 36 additions & 6 deletions src/com/loopj/android/http/AsyncHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,7 +77,7 @@
* For example:
* <p>
* <pre>
* AsyncHttpClient client = new AsyncHttpClient("My User Agent");
* AsyncHttpClient client = new AsyncHttpClient();
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
* &#064;Override
* public void onSuccess(String response) {
Expand All @@ -86,10 +87,12 @@
* </pre>
*/
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";

Expand All @@ -101,11 +104,11 @@ public class AsyncHttpClient {
private ThreadPoolExecutor threadPool;
private Map<Context, List<WeakReference<Future>>> 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);
Expand All @@ -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));
Expand Down Expand Up @@ -155,6 +158,15 @@ public void process(HttpResponse response, HttpContext context) {
requestMap = new WeakHashMap<Context, List<WeakReference<Future>>>();
}

/**
* 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}
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/com/loopj/android/http/AsyncHttpResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* For example:
* <p>
* <pre>
* AsyncHttpClient client = new AsyncHttpClient("My User Agent");
* AsyncHttpClient client = new AsyncHttpClient();
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
* &#064;Override
* public void onStart() {
Expand Down
2 changes: 1 addition & 1 deletion src/com/loopj/android/http/RequestParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
* </pre>
*/
Expand Down

0 comments on commit 177281d

Please sign in to comment.