Skip to content

Commit ef0c788

Browse files
author
Alex.K
committed
Merge branch 'pr/n1_tixsys'
Conflicts: src/com/loopj/android/http/AsyncHttpRequest.java src/com/loopj/android/http/RequestParams.java
2 parents 7853021 + a114b3f commit ef0c788

12 files changed

+520
-279
lines changed

.classpath

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
4-
<classpathentry kind="src" path="src"/>
5-
<classpathentry kind="src" path="gen"/>
6-
<classpathentry kind="src" path="examples"/>
7-
<classpathentry kind="output" path="bin/classes"/>
8-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="src" path="gen"/>
6+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
7+
<classpathentry kind="output" path="bin/classes"/>
8+
</classpath>

examples/ExampleUsage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class ExampleUsage {
44
public static void makeRequest() {
55
AsyncHttpClient client = new AsyncHttpClient();
66

7-
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
7+
client.get("http://www.google.com", new TextHttpResponseHandler() {
88
@Override
99
public void onSuccess(String response) {
1010
System.out.println(response);

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@
8181
* <p>
8282
* <pre>
8383
* AsyncHttpClient client = new AsyncHttpClient();
84-
* client.get("http://www.google.com", new AsyncHttpResponseHandler() {
84+
* client.get("http://www.google.com", new TextHttpResponseHandler() {
8585
* &#064;Override
86-
* public void onSuccess(String response) {
87-
* System.out.println(response);
86+
* public void onSuccess(String responseBody) {
87+
* System.out.println(responseBody);
8888
* }
8989
* });
9090
* </pre>
@@ -95,6 +95,7 @@ public class AsyncHttpClient {
9595
private static final int DEFAULT_MAX_CONNECTIONS = 10;
9696
private static final int DEFAULT_SOCKET_TIMEOUT = 10 * 1000;
9797
private static final int DEFAULT_MAX_RETRIES = 5;
98+
private static final int DEFAULT_RETRY_SLEEP_TIME_MILLIS = 1500;
9899
private static final int DEFAULT_SOCKET_BUFFER_SIZE = 8192;
99100
private static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
100101
private static final String ENCODING_GZIP = "gzip";
@@ -113,6 +114,14 @@ public class AsyncHttpClient {
113114
* Creates a new AsyncHttpClient.
114115
*/
115116
public AsyncHttpClient() {
117+
this(DEFAULT_MAX_RETRIES, DEFAULT_RETRY_SLEEP_TIME_MILLIS);
118+
}
119+
120+
public AsyncHttpClient(int maxRetries) {
121+
this(maxRetries, DEFAULT_RETRY_SLEEP_TIME_MILLIS);
122+
}
123+
124+
public AsyncHttpClient(int maxRetries, int retrySleepTimeMS) {
116125
BasicHttpParams httpParams = new BasicHttpParams();
117126

118127
ConnManagerParams.setTimeout(httpParams, socketTimeout);
@@ -163,7 +172,9 @@ public void process(HttpResponse response, HttpContext context) {
163172
}
164173
});
165174

166-
httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES));
175+
if(maxRetries < 0) maxRetries = DEFAULT_MAX_RETRIES;
176+
if(retrySleepTimeMS <0) retrySleepTimeMS = DEFAULT_RETRY_SLEEP_TIME_MILLIS;
177+
httpClient.setHttpRequestRetryHandler(new RetryHandler(maxRetries, retrySleepTimeMS));
167178

168179
threadPool = (ThreadPoolExecutor)Executors.newCachedThreadPool();
169180

src/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
package com.loopj.android.http;
2020

2121
import java.io.IOException;
22-
import java.net.ConnectException;
23-
import java.net.SocketException;
24-
import java.net.SocketTimeoutException;
2522
import java.net.UnknownHostException;
2623

2724
import org.apache.http.HttpResponse;
@@ -49,26 +46,20 @@ public AsyncHttpRequest(AbstractHttpClient client, HttpContext context, HttpUriR
4946
}
5047

5148
public void run() {
52-
try {
53-
if(responseHandler != null){
54-
responseHandler.sendStartMessage();
55-
}
49+
if (responseHandler != null) {
50+
responseHandler.sendStartMessage();
51+
}
5652

53+
try {
5754
makeRequestWithRetries();
58-
59-
if(responseHandler != null) {
60-
responseHandler.sendFinishMessage();
61-
}
6255
} catch (IOException e) {
63-
if(responseHandler != null) {
64-
responseHandler.sendFinishMessage();
65-
if(this.isBinaryRequest) {
66-
responseHandler.sendFailureMessage(e, (byte[]) null);
67-
} else {
68-
responseHandler.sendFailureMessage(e, (String) null);
69-
}
56+
if (responseHandler != null) {
57+
responseHandler.sendFailureMessage(0, null, e);
7058
}
7159
}
60+
if (responseHandler != null) {
61+
responseHandler.sendFinishMessage();
62+
}
7263
}
7364

7465
private void makeRequest() throws IOException {
@@ -90,47 +81,44 @@ private void makeRequest() throws IOException {
9081
}
9182
}
9283

93-
private void makeRequestWithRetries() throws ConnectException {
84+
private void makeRequestWithRetries() throws IOException {
9485
// This is an additional layer of retry logic lifted from droid-fu
9586
// See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
9687
boolean retry = true;
9788
IOException cause = null;
9889
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
99-
while (retry) {
100-
try {
101-
makeRequest();
102-
return;
103-
} catch (UnknownHostException e) {
104-
if(responseHandler != null) {
105-
responseHandler.sendFailureMessage(e, "can't resolve host");
106-
}
107-
return;
108-
}catch (SocketException e){
109-
// Added to detect host unreachable
110-
if(responseHandler != null) {
111-
responseHandler.sendFailureMessage(e, "can't resolve host");
90+
try
91+
{
92+
while (retry) {
93+
try {
94+
makeRequest();
95+
return;
96+
} catch (UnknownHostException e) {
97+
// switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
98+
// while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
99+
// (to assist in genuine cases of unknown host) which seems better than outright failure
100+
cause = new IOException("UnknownHostException exception: " + e.getMessage());
101+
retry = (executionCount > 0) && retryHandler.retryRequest(cause, ++executionCount, context);
102+
} catch (IOException e) {
103+
cause = e;
104+
retry = retryHandler.retryRequest(cause, ++executionCount, context);
105+
} catch (NullPointerException e) {
106+
// there's a bug in HttpClient 4.0.x that on some occasions causes
107+
// DefaultRequestExecutor to throw an NPE, see
108+
// http://code.google.com/p/android/issues/detail?id=5255
109+
cause = new IOException("NPE in HttpClient: " + e.getMessage());
110+
retry = retryHandler.retryRequest(cause, ++executionCount, context);
112111
}
113-
return;
114-
}catch (SocketTimeoutException e){
115-
if(responseHandler != null) {
116-
responseHandler.sendFailureMessage(e, "socket time out");
112+
if(retry && (responseHandler != null)) {
113+
responseHandler.sendRetryMessage();
117114
}
118-
return;
119-
} catch (IOException e) {
120-
cause = e;
121-
retry = retryHandler.retryRequest(cause, ++executionCount, context);
122-
} catch (NullPointerException e) {
123-
// there's a bug in HttpClient 4.0.x that on some occasions causes
124-
// DefaultRequestExecutor to throw an NPE, see
125-
// http://code.google.com/p/android/issues/detail?id=5255
126-
cause = new IOException("NPE in HttpClient" + e.getMessage());
127-
retry = retryHandler.retryRequest(cause, ++executionCount, context);
128115
}
116+
} catch (Exception e) {
117+
// catch anything else to ensure failure message is propagated
118+
cause = new IOException("Unhandled exception: " + e.getMessage());
129119
}
130-
131-
// no retries left, crap out with exception
132-
ConnectException ex = new ConnectException();
133-
ex.initCause(cause);
134-
throw ex;
120+
121+
// cleaned up to throw IOException
122+
throw(cause);
135123
}
136124
}

0 commit comments

Comments
 (0)