Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Using AsyncHttpClient from an IntentService #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
suneets opened this issue Mar 21, 2014 · 15 comments
Closed

Using AsyncHttpClient from an IntentService #496

suneets opened this issue Mar 21, 2014 · 15 comments
Assignees
Labels
Milestone

Comments

@suneets
Copy link

suneets commented Mar 21, 2014

I'm using AsyncHttpClient in a large Android project-- it works great in most cases, except when we need to make network calls from an Intent Service.

In this case-- we actually need to make a synchronous network call but the library doesn't make it easy to do that.

Also, we use session cookies with our AsyncHttpClient-- we create a static AsyncHttpClient instance and it's used throughout the project, and since we have a session cookie, this is preserved.

So I guess there are two potential solutions:

  1. Need a way to create a SyncHttpClient from an instance of AsyncHttpClient so session cookies are preserved

  2. Need a way to reliably use AsyncHttpClient in an IntentService

I know some other people have had issues with using this library in an IntentService-- how have you solved it?

@suneets
Copy link
Author

suneets commented Mar 21, 2014

I'm trying to use SyncHttpClient inside the intent service.. but I'm still getting this error even using SyncHttpClient. I'm using the latest from master branch ( built a jar today).

03-21 13:44:56.011  10710-10762/com.awear.android.debug E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:197)
            at android.os.Handler.<init>(Handler.java:111)
            at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.<init>(AsyncHttpResponseHandler.java:128)
            at com.loopj.android.http.AsyncHttpResponseHandler.<init>(AsyncHttpResponseHandler.java:167)
            at com.loopj.android.http.TextHttpResponseHandler.<init>(TextHttpResponseHandler.java:56)
            at com.loopj.android.http.JsonHttpResponseHandler.<init>(JsonHttpResponseHandler.java:45)
            at com.awear.server.AWJSONHttpResponseHandler.<init>(AWJSONHttpResponseHandler.java:16)
            at com.awear.server.ServerHelper$1.<init>(ServerHelper.java:41)
            at com.awear.server.ServerHelper.sendUserUpdate(ServerHelper.java:41)
            at com.awear.background.GcmRegistrationService.sendRegistrationIdToBackend(GcmRegistrationService.java:154)
            at com.awear.background.GcmRegistrationService.access$000(GcmRegistrationService.java:30)
            at com.awear.background.GcmRegistrationService$1.doInBackground(GcmRegistrationService.java:125)
            at com.awear.background.GcmRegistrationService$1.doInBackground(GcmRegistrationService.java:112)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)

@smarek
Copy link
Member

smarek commented Mar 24, 2014

This should be fixed by merge #492
Please test with latest repository version

@smarek smarek added this to the 1.4.5 milestone Mar 24, 2014
@smarek smarek self-assigned this Mar 24, 2014
@suneets
Copy link
Author

suneets commented Mar 25, 2014

I was able to get it to work with the latest version. In some cases I did need to call Looper.prepare() before making the async request.

@smarek
Copy link
Member

smarek commented Mar 25, 2014

Yes, there was another recent fix of Looper problem, see 989b35a

Thank you for testing, closing as solved

@smarek smarek closed this as completed Mar 25, 2014
@suneets
Copy link
Author

suneets commented Apr 2, 2014

The latest changes in the past week have now broken this... inside an intent service I can't make calls to AsyncHttpClient, I get the following exception:

It was working properly with the snapshot about a week ago. This is wit the latest as of today:

04-01 18:13:37.684    1912-2459/com.awear.android.debug E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[ActivityRecognitionIntentService]
    java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.
            at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:990)
            at com.loopj.android.http.AsyncHttpClient.put(AsyncHttpClient.java:905)
            at com.awear.AWHTTPClient.putJSONHelper(AWHTTPClient.java:157)
            at com.awear.AWHTTPClient.putJSONSynchronous(AWHTTPClient.java:143)
            at com.awear.background.ActivityRecognitionIntentService.sendMovementStateToServer(ActivityRecognitionIntentService.java:128)
            at com.awear.background.ActivityRecognitionIntentService.sendMovement(ActivityRecognitionIntentService.java:76)
            at com.awear.background.ActivityRecognitionIntentService.onHandleIntent(ActivityRecognitionIntentService.java:63)
            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.os.HandlerThread.run(HandlerThread.java:60)

@suneets
Copy link
Author

suneets commented Apr 2, 2014

I am also getting this:

04-02 12:03:12.618  22928-23420/com.awear.android.debug W/MessageQueue﹕ Handler (com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler) {4194e620} sending message to a Handler on a dead thread
    java.lang.RuntimeException: Handler (com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler) {4194e620} 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.post(Handler.java:323)
            at com.loopj.android.http.AsyncHttpResponseHandler.postRunnable(AsyncHttpResponseHandler.java:326)
            at com.loopj.android.http.JsonHttpResponseHandler$1.run(JsonHttpResponseHandler.java:121)
            at java.lang.Thread.run(Thread.java:856)

@smarek
Copy link
Member

smarek commented Apr 10, 2014

Is it hard to read the error message You should create your response handler in a looper thread or use SyncHttpClient instead. ?

@lbt05
Copy link

lbt05 commented Jul 8, 2014

Is there any sample code with that ? I read the Service sample but it's using SyncHttpClient...

@smarek
Copy link
Member

smarek commented Jul 8, 2014

Yes, it is, see the file: ExampleIntentService.java and the Activity context for that sample, IntentServiceSample.java

@dhams
Copy link

dhams commented Aug 14, 2014

Error message say itself solution "user You should create your response handler in a looper thread or use SyncHttpClient instead."

@smarek
Copy link
Member

smarek commented Aug 14, 2014

@dhams correct, thanks

@tedyyu
Copy link

tedyyu commented Dec 27, 2014

Sorry to dig into this old issue. I called AsyncHttpClient from a IntentService thread, if the client is initialized by new AsyncHttpClient, the response handler only executes onPreProcessResponse() callback, no onStart, onFinish, neither onSuccess nor onFailure. I was stuck by this problem and had a very long-time (few hours) search and finally saw this, my luck. After I change the client object to a new SyncHttpClient, then all callback are executed in the correct order, but the request will be blocked after the response has been handled. My question is can I still alternate to use AsyncHttpClient with non-blocking behavior in an intent service? Sorry if it's a silly question.

@dhams
Copy link

dhams commented Jan 9, 2015

@tedyyu The issue with using AsyncHttpClient insider IntentService is quite visible to me . IntentService manages queue of background thread and AsyncHttpClient is run itself in the thread so ultimatily you are calling thread insider the thread.
If you want to use AsyncHttpClient then you should use Service instead of IntentService .

@rodmiranda
Copy link

I was using 1.4.6 with both asynchronous and synchronous calls from a thread.
I had many problems and had to change my layer a couple of times. On one case, I wanted to start an async call from a running thread - I had my reasons - and was getting the exception below:

java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.

Then I have overridden the method mentioned in the answer below and all started to work.
http://stackoverflow.com/a/26747653/1713063

@lbt05
Copy link

lbt05 commented Jan 28, 2015

In 1.4.6, you can create ResponseHandler with a Looper param...I don't think you need to override the method to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants