Skip to content

Commit eccbce2

Browse files
committed
Merge pull request android-async-http#70 from alain57/master
added an onFailure if there is a problem with the remote host or the internet connection
2 parents bf30728 + 514d160 commit eccbce2

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.net.ConnectException;
23+
import java.net.UnknownHostException;
2324

2425
import org.apache.http.HttpResponse;
2526
import org.apache.http.client.HttpRequestRetryHandler;
@@ -91,6 +92,11 @@ private void makeRequestWithRetries() throws ConnectException {
9192
try {
9293
makeRequest();
9394
return;
95+
} catch (UnknownHostException e) {
96+
if(responseHandler != null) {
97+
responseHandler.sendFailureMessage(e, "can't resolve host");
98+
}
99+
return;
94100
} catch (IOException e) {
95101
cause = e;
96102
retry = retryHandler.retryRequest(cause, ++executionCount, context);
@@ -108,4 +114,4 @@ private void makeRequestWithRetries() throws ConnectException {
108114
ex.initCause(cause);
109115
throw ex;
110116
}
111-
}
117+
}

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,24 +106,32 @@ protected void handleSuccessJsonMessage(Object jsonResponse) {
106106
}
107107

108108
protected Object parseResponse(String responseBody) throws JSONException {
109-
return new JSONTokener(responseBody).nextValue();
109+
Object result = null;
110+
//trim the string to prevent start with blank, and test if the string is valid JSON, because the parser don't do this :(. If Json is not valid this will return null
111+
responseBody = responseBody.trim();
112+
if(responseBody.startsWith("{") || responseBody.startsWith("[")) {
113+
result = new JSONTokener(responseBody).nextValue();
114+
}
115+
return result;
110116
}
111117

112118
@Override
113119
protected void handleFailureMessage(Throwable e, String responseBody) {
114-
if (responseBody != null) try {
115-
Object jsonResponse = parseResponse(responseBody);
116-
if(jsonResponse instanceof JSONObject) {
117-
onFailure(e, (JSONObject)jsonResponse);
118-
} else if(jsonResponse instanceof JSONArray) {
119-
onFailure(e, (JSONArray)jsonResponse);
120+
try {
121+
if (responseBody != null) {
122+
Object jsonResponse = parseResponse(responseBody);
123+
if(jsonResponse instanceof JSONObject) {
124+
onFailure(e, (JSONObject)jsonResponse);
125+
} else if(jsonResponse instanceof JSONArray) {
126+
onFailure(e, (JSONArray)jsonResponse);
127+
} else {
128+
onFailure(e, responseBody);
129+
}
130+
}else {
131+
onFailure(e, "");
120132
}
121-
}
122-
catch(JSONException ex) {
133+
}catch(JSONException ex) {
123134
onFailure(e, responseBody);
124135
}
125-
else {
126-
onFailure(e, "");
127-
}
128136
}
129137
}

0 commit comments

Comments
 (0)