Skip to content

Commit 514d160

Browse files
committed
AsyncHttpRequest now knows if the host is unknown (down or internet down)
Further verification on the JSon format of the response Handler.
1 parent cff7425 commit 514d160

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
@@ -78,7 +78,13 @@ protected void handleSuccessMessage(String responseBody) {
7878
}
7979

8080
protected Object parseResponse(String responseBody) throws JSONException {
81-
return new JSONTokener(responseBody).nextValue();
81+
Object result = null;
82+
//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
83+
responseBody = responseBody.trim();
84+
if(responseBody.startsWith("{") || responseBody.startsWith("[")) {
85+
result = new JSONTokener(responseBody).nextValue();
86+
}
87+
return result;
8288
}
8389

8490
/**
@@ -89,19 +95,21 @@ public void onFailure(Throwable e, JSONArray errorResponse) {}
8995

9096
@Override
9197
protected void handleFailureMessage(Throwable e, String responseBody) {
92-
if (responseBody != null) try {
93-
Object jsonResponse = parseResponse(responseBody);
94-
if(jsonResponse instanceof JSONObject) {
95-
onFailure(e, (JSONObject)jsonResponse);
96-
} else if(jsonResponse instanceof JSONArray) {
97-
onFailure(e, (JSONArray)jsonResponse);
98+
try {
99+
if (responseBody != null) {
100+
Object jsonResponse = parseResponse(responseBody);
101+
if(jsonResponse instanceof JSONObject) {
102+
onFailure(e, (JSONObject)jsonResponse);
103+
} else if(jsonResponse instanceof JSONArray) {
104+
onFailure(e, (JSONArray)jsonResponse);
105+
} else {
106+
onFailure(e, responseBody);
107+
}
108+
}else {
109+
onFailure(e, "");
98110
}
99-
}
100-
catch(JSONException ex) {
111+
}catch(JSONException ex) {
101112
onFailure(e, responseBody);
102113
}
103-
else {
104-
onFailure(e, "");
105-
}
106114
}
107115
}

0 commit comments

Comments
 (0)