Skip to content

Commit

Permalink
Merge pull request android-async-http#507 from EugenePogrebnoy/master
Browse files Browse the repository at this point in the history
Fixed AsyncHttpResponseHandler to properly work on threads without Looper
  • Loading branch information
smarek committed Mar 28, 2014
2 parents 989b35a + 3ac8d50 commit 78e137a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,10 @@ public RequestHandle delete(Context context, String url, Header[] headers, Reque
* @return RequestHandle of future request process
*/
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
if (responseHandler != null && responseHandler.getUseSynchronousMode()) {
throw new IllegalArgumentException("Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
}

if (contentType != null) {
uriRequest.setHeader("Content-Type", contentType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,16 @@ public String getCharset() {
* Creates a new AsyncHttpResponseHandler
*/
public AsyncHttpResponseHandler() {
// There is always a handler ready for delivering messages.
handler = new ResponderHandler(this);
boolean missingLooper = null == Looper.myLooper();
// Try to create handler
if (!missingLooper)
handler = new ResponderHandler(this);
else {
// There is no Looper on this thread so synchronous mode should be used.
handler = null;
setUseSynchronousMode(true);
Log.i(LOG_TAG, "Current thread has not called Looper.prepare(). Forcing synchronous mode.");
}

// Init Looper by calling postRunnable without an argument.
postRunnable(null);
Expand Down Expand Up @@ -319,14 +327,13 @@ protected void sendMessage(Message msg) {
* @param runnable runnable instance, can be null
*/
protected void postRunnable(Runnable runnable) {
boolean missingLooper = null == Looper.myLooper();
if (runnable != null) {
if (missingLooper) {
// If there is no looper, run on provided handler
handler.post(runnable);
if (getUseSynchronousMode()){
// This response handler is synchronous, run on current thread
runnable.run();
} else {
// Otherwise, run on current thread
runnable.run();
// Otherwise, run on provided handler
handler.post(runnable);
}
}
}
Expand All @@ -339,7 +346,7 @@ protected void postRunnable(Runnable runnable) {
* @return Message instance, should not be null
*/
protected Message obtainMessage(int responseMessageId, Object responseMessageData) {
return handler.obtainMessage(responseMessageId, responseMessageData);
return Message.obtain(handler, responseMessageId, responseMessageData);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public BaseJsonHttpResponseHandler(String encoding) {
@Override
public final void onSuccess(final int statusCode, final Header[] headers, final String responseString) {
if (statusCode != HttpStatus.SC_NO_CONTENT) {
new Thread(new Runnable() {
Runnable parser = new Runnable() {
@Override
public void run() {
try {
Expand All @@ -95,7 +95,11 @@ public void run() {
});
}
}
}).start();
};
if (!getUseSynchronousMode())
new Thread(parser).start();
else // In synchronous mode everything should be run on one thread
parser.run();
} else {
onSuccess(statusCode, headers, null, null);
}
Expand All @@ -104,7 +108,7 @@ public void run() {
@Override
public final void onFailure(final int statusCode, final Header[] headers, final String responseString, final Throwable throwable) {
if (responseString != null) {
new Thread(new Runnable() {
Runnable parser = new Runnable() {
@Override
public void run() {
try {
Expand All @@ -125,7 +129,11 @@ public void run() {
});
}
}
}).start();
};
if (!getUseSynchronousMode())
new Thread(parser).start();
else // In synchronous mode everything should be run on one thread
parser.run();
} else {
onFailure(statusCode, headers, throwable, null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void onSuccess(int statusCode, Header[] headers, String responseString) {
@Override
public final void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBytes) {
if (statusCode != HttpStatus.SC_NO_CONTENT) {
new Thread(new Runnable() {
Runnable parser = new Runnable() {
@Override
public void run() {
try {
Expand Down Expand Up @@ -142,7 +142,11 @@ public void run() {
});
}
}
}).start();
};
if (!getUseSynchronousMode())
new Thread(parser).start();
else // In synchronous mode everything should be run on one thread
parser.run();
} else {
onSuccess(statusCode, headers, new JSONObject());
}
Expand All @@ -151,7 +155,7 @@ public void run() {
@Override
public final void onFailure(final int statusCode, final Header[] headers, final byte[] responseBytes, final Throwable throwable) {
if (responseBytes != null) {
new Thread(new Runnable() {
Runnable parser = new Runnable() {
@Override
public void run() {
try {
Expand Down Expand Up @@ -181,7 +185,11 @@ public void run() {

}
}
}).start();
};
if (!getUseSynchronousMode())
new Thread(parser).start();
else // In synchronous mode everything should be run on one thread
parser.run();
} else {
Log.v(LOG_TAG, "response body is null, calling onFailure(Throwable, JSONObject)");
onFailure(statusCode, headers, throwable, (JSONObject) null);
Expand Down

0 comments on commit 78e137a

Please sign in to comment.