Skip to content

Conversation

suneets
Copy link

@suneets suneets commented Apr 5, 2014

This change allows you to submit a synchronous request to AsyncHttpClient.

It's an ideal solution for making network requests inside an IntentService because it lets you:

  • Keep 1 static AsyncHttpClient
  • Make mostly async network requests
  • Make synchronous network requests inside an IntentService where synchronous is necessary

We use AsyncHttpClient heavily in our project. We use Session cookies in our headers which get stored in AsyncHttpClient. Because we use session cookies, we can't keep both a SyncHttpClient, and an AsyncHttpClient.. we need one client from which we can make both synchronous and asynchronous requests.

90% of our requests are Async. The reason we need synchronous requests is as follows:

  • Our project uses a couple Android IntentService classes
  • (Some examples are periodically fetching user location, user activity, and perform GCM registration
  • We need to make network requests inside an IntentService, which creates its own thread
  • Performing a synchronous request inside an IntentService (which is already a background thread) guarantees that our RequestHandler will get called

Making an asynchronous request inside an IntentService results in the following dead thread exception, hence this pull request.

4-04 18:14:01.491     885-1356/com.awear.android.debug W/MessageQueue﹕ Handler (com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler) {41ac0348} sending message to a Handler on a dead thread
    java.lang.RuntimeException: Handler (com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler) {41ac0348} sending message to a Handler on a dead thread
            at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
            at android.os.Handler.enqueueMessage(Handler.java:618)
            at android.os.Handler.sendMessageAtTime(Handler.java:587)
            at android.os.Handler.sendMessageDelayed(Handler.java:558)
            at android.os.Handler.sendMessage(Handler.java:495)
            at com.loopj.android.http.AsyncHttpResponseHandler.sendMessage(AsyncHttpResponseHandler.java:320)
            at com.loopj.android.http.AsyncHttpResponseHandler.sendFinishMessage(AsyncHttpResponseHandler.java:253)
            at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:82)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)

@smarek
Copy link
Member

smarek commented Apr 10, 2014

To make synchronous request, you should use SyncHttpClient class, so please put a patch against it, I won't merge synchronous behavior against AsyncHttpClient class, it would confuse future developers.

@smarek smarek closed this Apr 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants