Skip to content

Commit eae48b8

Browse files
committed
Fixed AsyncHttpResponseHandler to properly work on threads without Looper
1 parent 989b35a commit eae48b8

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
978978
* @return RequestHandle of future request process
979979
*/
980980
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
981+
if (responseHandler != null && responseHandler.getUseSynchronousMode()) {
982+
throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
983+
}
984+
981985
if (contentType != null) {
982986
uriRequest.setHeader("Content-Type", contentType);
983987
}

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,16 @@ public String getCharset() {
163163
* Creates a new AsyncHttpResponseHandler
164164
*/
165165
public AsyncHttpResponseHandler() {
166-
// There is always a handler ready for delivering messages.
167-
handler = new ResponderHandler(this);
166+
boolean missingLooper = null == Looper.myLooper();
167+
// Try to create handler
168+
if (!missingLooper)
169+
handler = new ResponderHandler(this);
170+
else {
171+
// There is no Looper on this thread so synchronous mode should be used.
172+
handler = null;
173+
setUseSynchronousMode(true);
174+
Log.i(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
175+
}
168176

169177
// Init Looper by calling postRunnable without an argument.
170178
postRunnable(null);
@@ -319,14 +327,13 @@ protected void sendMessage(Message msg) {
319327
* @param runnable runnable instance, can be null
320328
*/
321329
protected void postRunnable(Runnable runnable) {
322-
boolean missingLooper = null == Looper.myLooper();
323330
if (runnable != null) {
324-
if (missingLooper) {
325-
// If there is no looper, run on provided handler
326-
handler.post(runnable);
331+
if (getUseSynchronousMode()){
332+
// This response handler is synchronous, run on current thread
333+
runnable.run();
327334
} else {
328-
// Otherwise, run on current thread
329-
runnable.run();
335+
// Otherwise, run on provided handler
336+
handler.post(runnable);
330337
}
331338
}
332339
}
@@ -339,7 +346,7 @@ protected void postRunnable(Runnable runnable) {
339346
* @return Message instance, should not be null
340347
*/
341348
protected Message obtainMessage(int responseMessageId, Object responseMessageData) {
342-
return handler.obtainMessage(responseMessageId, responseMessageData);
349+
return Message.obtain(handler, responseMessageId, responseMessageData);
343350
}
344351

345352
@Override

0 commit comments

Comments
 (0)