Skip to content

Commit 5217c5f

Browse files
committed
Merge branch 'master' into 2.0
2 parents 9917ff7 + 656a244 commit 5217c5f

37 files changed

+2255
-626
lines changed

.travis.yml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,23 @@ before_install:
1616
# for gradle output style
1717
- export TERM=dumb
1818
# newer version of gradle
19-
- wget http://services.gradle.org/distributions/gradle-1.8-bin.zip
20-
- unzip -qq gradle-1.8-bin.zip
21-
- export GRADLE_HOME=$PWD/gradle-1.8
19+
- wget http://services.gradle.org/distributions/gradle-1.9-bin.zip
20+
- unzip -qq gradle-1.9-bin.zip
21+
- export GRADLE_HOME=$PWD/gradle-1.9
2222
- export PATH=$GRADLE_HOME/bin:$PATH
2323
# just to test gradle version, against our provided one
2424
- gradle -v
25-
# newest android SDK 22.0.5
26-
- wget http://dl.google.com/android/android-sdk_r22.0.5-linux.tgz
27-
- tar -zxf android-sdk_r22.0.5-linux.tgz
25+
# newest android SDK 22.3
26+
- wget http://dl.google.com/android/android-sdk_r22.3-linux.tgz
27+
- tar -zxf android-sdk_r22.3-linux.tgz
2828
- export ANDROID_HOME=`pwd`/android-sdk-linux
2929
- export PATH=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
3030
# manually set sdk.dir variable, according to local paths
3131
- echo "sdk.dir=$ANDROID_HOME" > local.properties
32-
- echo yes | android update sdk -t tools,platform-tools,extra-android-support,extra-android-m2repository,android-18 --force --no-ui
33-
# build tools cannot be installed through "android update sdk" as of now
34-
- wget http://dl.google.com/android/repository/build-tools_r18.0.1-linux.zip
35-
- mkdir -p $ANDROID_HOME/build-tools
36-
- unzip -qq build-tools_r18.0.1-linux.zip -d $ANDROID_HOME/build-tools/
37-
- mv $ANDROID_HOME/build-tools/android-4.3 $ANDROID_HOME/build-tools/18.0.1
38-
# verify files exist in right paths
39-
- find $ANDROID_HOME/build-tools
40-
- file $ANDROID_HOME/build-tools/18.0.1/aapt
32+
# list packages for debug purpose
33+
- android list sdk -a -e -u
34+
- echo yes | android update sdk -t tools,platform-tools,extra-android-support,extra-android-m2repository,android-19,build-tools-19.0.0 --force --no-ui
35+
# Sonatype bypass
4136
- echo "nexusUsername=dummy" >> library/gradle.properties
4237
- echo "nexusPassword=dummy" >> library/gradle.properties
4338

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ https://oss.sonatype.org/content/repositories/snapshots/com/loopj/android/androi
2929
Maven URL: https://oss.sonatype.org/content/repositories/snapshots/
3030
GroupId: com.loopj.android
3131
ArtifactId: async-http-client
32-
Version: 1.4.4-SNAPSHOT
32+
Version: 1.4.5-SNAPSHOT
3333
Packaging: JAR or AAR
3434
```
3535

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
}
55

66
dependencies {
7-
classpath 'com.android.tools.build:gradle:0.6.+'
7+
classpath 'com.android.tools.build:gradle:0.7.+'
88
}
99
}
1010

@@ -14,7 +14,7 @@ def isReleaseBuild() {
1414

1515
allprojects {
1616
group = 'com.loopj.android'
17-
version = '1.4.4'
17+
version = '1.4.5-SNAPSHOT'
1818

1919
repositories {
2020
mavenCentral()

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
VERSION_NAME=1.4.4
2-
VERSION_CODE=144
1+
VERSION_NAME=1.4.5-SNAPSHOT
2+
VERSION_CODE=145
33
GROUP=com.loopj.android
44

55
POM_DESCRIPTION=An Asynchronous HTTP Library for Android

library/build.gradle

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
apply plugin: 'android-library'
22

33
android {
4-
compileSdkVersion 18
5-
buildToolsVersion '18.0.1'
4+
compileSdkVersion 19
5+
buildToolsVersion '19.0.0'
6+
7+
lintOptions {
8+
abortOnError false
9+
}
610
}
711

812
repositories {

library/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.loopj.android.http"
4-
android:versionName="1.4.4"
5-
android:versionCode="144">
4+
android:versionName="1.4.5-SNAPSHOT"
5+
android:versionCode="145">
66

77
<uses-sdk
88
android:minSdkVersion="3" />

library/src/main/java/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@
6363

6464
import java.io.IOException;
6565
import java.io.InputStream;
66-
import java.lang.ref.WeakReference;
6766
import java.util.HashMap;
67+
import java.util.Iterator;
6868
import java.util.LinkedList;
6969
import java.util.List;
7070
import java.util.Map;
7171
import java.util.WeakHashMap;
72+
import java.util.concurrent.ExecutorService;
7273
import java.util.concurrent.Executors;
73-
import java.util.concurrent.Future;
7474
import java.util.concurrent.ThreadPoolExecutor;
7575
import java.util.zip.GZIPInputStream;
7676

@@ -91,24 +91,24 @@
9191
* </pre>
9292
*/
9393
public class AsyncHttpClient {
94-
private static final String VERSION = "1.4.4";
9594

96-
private static final int DEFAULT_MAX_CONNECTIONS = 10;
97-
private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
98-
private static final int DEFAULT_MAX_RETRIES = 5;
99-
private static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500;
100-
private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
101-
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
102-
private static final String ENCODING_GZIP = "gzip";
103-
private static final String LOG_TAG = "AsyncHttpClient";
95+
public static final String VERSION = "1.4.5";
96+
public static final int DEFAULT_MAX_CONNECTIONS = 10;
97+
public static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
98+
public static final int DEFAULT_MAX_RETRIES = 5;
99+
public static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500;
100+
public static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
101+
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
102+
public static final String ENCODING_GZIP = "gzip";
103+
public static final String LOG_TAG = "AsyncHttpClient";
104104

105105
private int maxConnections = DEFAULT_MAX_CONNECTIONS;
106106
private int timeout = DEFAULT_SOCKET_TIMEOUT;
107107

108108
private final DefaultHttpClient httpClient;
109109
private final HttpContext httpContext;
110-
private ThreadPoolExecutor threadPool;
111-
private final Map<Context, List<WeakReference<Future<?>>>> requestMap;
110+
private ExecutorService threadPool;
111+
private final Map<Context, List<RequestHandle>> requestMap;
112112
private final Map<String, String> clientHeaderMap;
113113
private boolean isUrlEncodingEnabled = true;
114114

@@ -209,8 +209,8 @@ public AsyncHttpClient(SchemeRegistry schemeRegistry) {
209209

210210
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
211211

212-
threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(DEFAULT_MAX_CONNECTIONS);
213-
requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>();
212+
threadPool = Executors.newCachedThreadPool();
213+
requestMap = new WeakHashMap<Context, List<RequestHandle>>();
214214
clientHeaderMap = new HashMap<String, String>();
215215

216216
httpContext = new SyncBasicHttpContext(new BasicHttpContext());
@@ -249,6 +249,18 @@ public void process(HttpResponse response, HttpContext context) {
249249
httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES, DEFAULT_RETRY_SLEEP_TIME_MILLIS));
250250
}
251251

252+
public static void allowRetryExceptionClass(Class<?> cls) {
253+
if (cls != null) {
254+
RetryHandler.addClassToWhitelist(cls);
255+
}
256+
}
257+
258+
public static void blockRetryExceptionClass(Class<?> cls) {
259+
if (cls != null) {
260+
RetryHandler.addClassToBlacklist(cls);
261+
}
262+
}
263+
252264
/**
253265
* Get the underlying HttpClient instance. This is useful for setting additional fine-grained
254266
* settings for requests by accessing the client's ConnectionManager, HttpParams and
@@ -475,21 +487,16 @@ public void clearBasicAuth() {
475487
* pending requests.
476488
*/
477489
public void cancelRequests(Context context, boolean mayInterruptIfRunning) {
478-
List<WeakReference<Future<?>>> requestList = requestMap.get(context);
490+
List<RequestHandle> requestList = requestMap.get(context);
479491
if (requestList != null) {
480-
for (WeakReference<Future<?>> requestRef : requestList) {
481-
Future<?> request = requestRef.get();
482-
if (request != null) {
483-
request.cancel(mayInterruptIfRunning);
484-
}
492+
for (RequestHandle requestHandle : requestList) {
493+
requestHandle.cancel(mayInterruptIfRunning);
485494
}
495+
requestMap.remove(context);
486496
}
487-
requestMap.remove(context);
488497
}
489498

490-
//
491-
// HTTP HEAD Requests
492-
//
499+
// [+] HTTP HEAD
493500

494501
/**
495502
* Perform a HTTP HEAD request, without any parameters.
@@ -558,10 +565,8 @@ public RequestHandle head(Context context, String url, Header[] headers, Request
558565
context);
559566
}
560567

561-
562-
//
563-
// HTTP GET Requests
564-
//
568+
// [-] HTTP HEAD
569+
// [+] HTTP GET
565570

566571
/**
567572
* Perform a HTTP GET request, without any parameters.
@@ -630,10 +635,8 @@ public RequestHandle get(Context context, String url, Header[] headers, RequestP
630635
context);
631636
}
632637

633-
634-
//
635-
// HTTP POST Requests
636-
//
638+
// [-] HTTP GET
639+
// [+] HTTP POST
637640

638641
/**
639642
* Perform a HTTP POST request, without any parameters.
@@ -732,9 +735,8 @@ public RequestHandle post(Context context, String url, Header[] headers, HttpEnt
732735
return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
733736
}
734737

735-
//
736-
// HTTP PUT Requests
737-
//
738+
// [-] HTTP POST
739+
// [+] HTTP PUT
738740

739741
/**
740742
* Perform a HTTP PUT request, without any parameters.
@@ -811,9 +813,8 @@ public RequestHandle put(Context context, String url, Header[] headers, HttpEnti
811813
return sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
812814
}
813815

814-
//
815-
// HTTP DELETE Requests
816-
//
816+
// [-] HTTP PUT
817+
// [+] HTTP DELETE
817818

818819
/**
819820
* Perform a HTTP DELETE request.
@@ -870,6 +871,8 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
870871
return sendRequest(httpClient, httpContext, httpDelete, null, responseHandler, context);
871872
}
872873

874+
// [-] HTTP DELETE
875+
873876
/**
874877
* Puts a new request in queue as a new thread in pool to be executed
875878
*
@@ -884,28 +887,35 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
884887
*/
885888
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
886889
if (contentType != null) {
887-
uriRequest.addHeader("Content-Type", contentType);
890+
uriRequest.setHeader("Content-Type", contentType);
888891
}
889892

890893
responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
891894
responseHandler.setRequestURI(uriRequest.getURI());
892895

893-
Future<?> request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));
896+
AsyncHttpRequest request = new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler);
897+
threadPool.submit(request);
898+
RequestHandle requestHandle = new RequestHandle(request);
894899

895900
if (context != null) {
896901
// Add request to request map
897-
List<WeakReference<Future<?>>> requestList = requestMap.get(context);
902+
List<RequestHandle> requestList = requestMap.get(context);
898903
if (requestList == null) {
899-
requestList = new LinkedList<WeakReference<Future<?>>>();
904+
requestList = new LinkedList<RequestHandle>();
900905
requestMap.put(context, requestList);
901906
}
902907

903-
requestList.add(new WeakReference<Future<?>>(request));
908+
requestList.add(requestHandle);
904909

905-
// TODO: Remove dead weakrefs from requestLists?
910+
Iterator<RequestHandle> iterator = requestList.iterator();
911+
while (iterator.hasNext()) {
912+
if (iterator.next().shouldBeGarbageCollected()) {
913+
iterator.remove();
914+
}
915+
}
906916
}
907917

908-
return new RequestHandle(request);
918+
return requestHandle;
909919
}
910920

911921
/**

0 commit comments

Comments
 (0)