Skip to content

Commit 78e137a

Browse files
committed
Merge pull request android-async-http#507 from EugenePogrebnoy/master
Fixed AsyncHttpResponseHandler to properly work on threads without Looper
2 parents 989b35a + 3ac8d50 commit 78e137a

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

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

+4
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

+16-9
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

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public BaseJsonHttpResponseHandler(String encoding) {
7474
@Override
7575
public final void onSuccess(final int statusCode, final Header[] headers, final String responseString) {
7676
if (statusCode != HttpStatus.SC_NO_CONTENT) {
77-
new Thread(new Runnable() {
77+
Runnable parser = new Runnable() {
7878
@Override
7979
public void run() {
8080
try {
@@ -95,7 +95,11 @@ public void run() {
9595
});
9696
}
9797
}
98-
}).start();
98+
};
99+
if (!getUseSynchronousMode())
100+
new Thread(parser).start();
101+
else // In synchronous mode everything should be run on one thread
102+
parser.run();
99103
} else {
100104
onSuccess(statusCode, headers, null, null);
101105
}
@@ -104,7 +108,7 @@ public void run() {
104108
@Override
105109
public final void onFailure(final int statusCode, final Header[] headers, final String responseString, final Throwable throwable) {
106110
if (responseString != null) {
107-
new Thread(new Runnable() {
111+
Runnable parser = new Runnable() {
108112
@Override
109113
public void run() {
110114
try {
@@ -125,7 +129,11 @@ public void run() {
125129
});
126130
}
127131
}
128-
}).start();
132+
};
133+
if (!getUseSynchronousMode())
134+
new Thread(parser).start();
135+
else // In synchronous mode everything should be run on one thread
136+
parser.run();
129137
} else {
130138
onFailure(statusCode, headers, throwable, null, null);
131139
}

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void onSuccess(int statusCode, Header[] headers, String responseString) {
113113
@Override
114114
public final void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBytes) {
115115
if (statusCode != HttpStatus.SC_NO_CONTENT) {
116-
new Thread(new Runnable() {
116+
Runnable parser = new Runnable() {
117117
@Override
118118
public void run() {
119119
try {
@@ -142,7 +142,11 @@ public void run() {
142142
});
143143
}
144144
}
145-
}).start();
145+
};
146+
if (!getUseSynchronousMode())
147+
new Thread(parser).start();
148+
else // In synchronous mode everything should be run on one thread
149+
parser.run();
146150
} else {
147151
onSuccess(statusCode, headers, new JSONObject());
148152
}
@@ -151,7 +155,7 @@ public void run() {
151155
@Override
152156
public final void onFailure(final int statusCode, final Header[] headers, final byte[] responseBytes, final Throwable throwable) {
153157
if (responseBytes != null) {
154-
new Thread(new Runnable() {
158+
Runnable parser = new Runnable() {
155159
@Override
156160
public void run() {
157161
try {
@@ -181,7 +185,11 @@ public void run() {
181185

182186
}
183187
}
184-
}).start();
188+
};
189+
if (!getUseSynchronousMode())
190+
new Thread(parser).start();
191+
else // In synchronous mode everything should be run on one thread
192+
parser.run();
185193
} else {
186194
Log.v(LOG_TAG, "response body is null, calling onFailure(Throwable, JSONObject)");
187195
onFailure(statusCode, headers, throwable, (JSONObject) null);

0 commit comments

Comments
 (0)