Skip to content

Commit dfd8661

Browse files
committed
Rebasing to latest commit from upstream
1 parent 6fe5ce6 commit dfd8661

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

library/src/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public AsyncHttpResponseHandler() {
161161
* Fired when the request progress, override to handle in your own code
162162
*
163163
* @param bytesWritten offset from start of file
164-
* @param totalSize total size of file
164+
* @param totalSize total size of file
165165
*/
166166
public void onProgress(int bytesWritten, int totalSize) {
167167
}
@@ -373,6 +373,12 @@ protected void sendMessage(Message msg) {
373373
}
374374
}
375375

376+
protected void postRunnable(Runnable r) {
377+
if (r != null) {
378+
handler.post(r);
379+
}
380+
}
381+
376382
protected Message obtainMessage(int responseMessage, Object response) {
377383
Message msg;
378384
if (handler != null) {

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

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,29 @@ public void onSuccess(final int statusCode, final Header[] headers, final byte[]
146146
@Override
147147
public void run() {
148148
try {
149-
Object jsonResponse = parseResponse(responseBody);
150-
if (jsonResponse instanceof JSONObject) {
151-
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
152-
} else if (jsonResponse instanceof JSONArray) {
153-
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
154-
} else if (jsonResponse instanceof String) {
155-
onSuccess(statusCode, headers, (String) jsonResponse);
156-
} else {
157-
onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject) null);
158-
}
159-
} catch (JSONException ex) {
160-
onFailure(ex, (JSONObject) null);
149+
final Object jsonResponse = parseResponse(responseBody);
150+
postRunnable(new Runnable() {
151+
@Override
152+
public void run() {
153+
if (jsonResponse instanceof JSONObject) {
154+
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
155+
} else if (jsonResponse instanceof JSONArray) {
156+
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
157+
} else if (jsonResponse instanceof String) {
158+
onSuccess(statusCode, headers, (String) jsonResponse);
159+
} else {
160+
onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject) null);
161+
}
162+
163+
}
164+
});
165+
} catch (final JSONException ex) {
166+
postRunnable(new Runnable() {
167+
@Override
168+
public void run() {
169+
onFailure(ex, (JSONObject) null);
170+
}
171+
});
161172
}
162173
}
163174
}).start();
@@ -168,30 +179,41 @@ public void run() {
168179

169180
@Override
170181
public void onFailure(final int statusCode, final Header[] headers, final byte[] responseBody, final Throwable e) {
171-
new Thread(new Runnable() {
172-
@Override
173-
public void run() {
174-
try {
175-
if (responseBody != null) {
176-
Object jsonResponse = parseResponse(responseBody);
177-
if (jsonResponse instanceof JSONObject) {
178-
onFailure(statusCode, headers, e, (JSONObject) jsonResponse);
179-
} else if (jsonResponse instanceof JSONArray) {
180-
onFailure(statusCode, headers, e, (JSONArray) jsonResponse);
181-
} else if (jsonResponse instanceof String) {
182-
onFailure(statusCode, headers, e, (String) jsonResponse);
183-
} else {
184-
onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject) null);
185-
}
186-
} else {
187-
onFailure(e, "");
182+
if (responseBody != null) {
183+
new Thread(new Runnable() {
184+
@Override
185+
public void run() {
186+
try {
187+
final Object jsonResponse = parseResponse(responseBody);
188+
postRunnable(new Runnable() {
189+
@Override
190+
public void run() {
191+
if (jsonResponse instanceof JSONObject) {
192+
onFailure(statusCode, headers, e, (JSONObject) jsonResponse);
193+
} else if (jsonResponse instanceof JSONArray) {
194+
onFailure(statusCode, headers, e, (JSONArray) jsonResponse);
195+
} else if (jsonResponse instanceof String) {
196+
onFailure(statusCode, headers, e, (String) jsonResponse);
197+
} else {
198+
onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject) null);
199+
}
200+
}
201+
});
202+
203+
} catch (final JSONException ex) {
204+
postRunnable(new Runnable() {
205+
@Override
206+
public void run() {
207+
onFailure(ex, (JSONObject) null );
208+
}
209+
});
210+
188211
}
189-
} catch (JSONException ex) {
190-
onFailure(ex, (JSONObject) null);
191212
}
192-
}
193-
}).start();
194-
213+
}).start();
214+
} else {
215+
onFailure(e, "");
216+
}
195217
}
196218

197219
protected Object parseResponse(byte[] responseBody) throws JSONException {

0 commit comments

Comments
 (0)