Skip to content

1.4.5 Regression in AsynHttpResponseHandler (bug ?) #633

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
neoteknic opened this issue Jul 1, 2014 · 9 comments
Closed

1.4.5 Regression in AsynHttpResponseHandler (bug ?) #633

neoteknic opened this issue Jul 1, 2014 · 9 comments

Comments

@neoteknic
Copy link

Hello,
I don't understand something about the new 1.4.5 version, the previous one 1.4.4 was wordking fine !
With the new API I just changed the onSucces (previously onSuccess(String response), now with 3 parameters

I Have a class:

public class Webservice extends Thread
{
   private AsyncHttpClient  httpClient;
   public Webservice(Activity a)
    {
      httpClient = new AsyncHttpClient();
      ...
    }

    @Override
     public void run()
     {
        ....
        httpClient.post(activity.getString(R.string.url_api),params,new AsyncHttpResponseHandler( ){

          @Override
          public void onSuccess(int statusCode, Header[] headers, byte[] response)
          {
          }
        });
     }
}

Now I have error : current thead has not called Looper.prepare() Forcing synchronous mode;
Tried to add Looper.prepare in the construcor but crash the app.
and another error Synchronous ResponsHangdler in AsyncHttpClient.
Maybe the fix is just to remove the check for Looper.prepare==null ?
I dont really undestand this but.
I think it's related to this change : Fixed Threading issue when used in Service or non-UI Thread context
Because I use it in a thread. But can you add an optino to force async mode, or should I use sync mode ?

@fineswap fineswap added the low label Jul 2, 2014
@fineswap fineswap added this to the 1.4.6 milestone Jul 2, 2014
@fineswap fineswap self-assigned this Jul 2, 2014
@fineswap
Copy link
Contributor

fineswap commented Jul 2, 2014

@neoteknic

That was not an error, it was a warning. And if when you add a Looper.prepare() the app crashes, then how is that connected to AHC? AHC works in both situations, with or without a handler, so your app needs to support either of these.

The difference between 1.4.4 and 1.4.5 is simple: in prior versions, AHC automatically created the looper when it didn't exist, which was alright in situations but not alright in other situations. So we removed it and now the developer needs to prepare a looper beforehand.

@smarek
Copy link
Member

smarek commented Jul 2, 2014

@neoteknic if you add Looper.prepare() what StackTrace do you see in console? Post it please.
Also, in your particular use-case, should the request be synchronous or asynchronous? Thanks

@lbt05
Copy link

lbt05 commented Jul 8, 2014

it's very weird to call a async http lib API in an async thread....

@smarek
Copy link
Member

smarek commented Jul 8, 2014

@lbt05 yes, that is not the way the library is meant to be used, however it fits someones needs, so we'd like to support them.

@neoteknic
Copy link
Author

Hello,
For Looper.prepare() if I call it it just crash the app.
It use to work in the previous version but as lbtp5 says I am already in a thread and in this case I dont need another thread. So I just changed a bit my code to use SyncHttp. The problem is that the new version dont allow this possibility in my Webservoce class I have also a method private void notify() called in onSuccess or after http post if if fail to catch the error. This method send a callback to an Activity or fragment class wich implement a notify listener.
No I dont need a thread in a thread, but why it was possible in the previous version, with the async with no looper prepare warning it swich to a sync http client but later you have a NPE because you use AHC instead of SyncHttpClient. Just produce a fatal error in the Looper.prepare test, not a warning maybe.

@smarek
Copy link
Member

smarek commented Jul 8, 2014

@neoteknic can you post the stacktraces?

@neoteknic
Copy link
Author

I will try to revert to my previous code to do that.

@smarek
Copy link
Member

smarek commented Aug 12, 2014

If you still suffer from the issue, send back the stacktraces.
In other way, you should use AsyncHttpClient aClient = new SyncHttpClient(); constructor

See this example of running requests in background thread:
https://github.com/loopj/android-async-http/blob/master/sample/src/main/java/com/loopj/android/http/sample/services/ExampleIntentService.java

@smarek smarek closed this as completed Aug 12, 2014
@smarek smarek added the on hold label Aug 12, 2014
@lucasjotab3
Copy link

I want an Async request. Do I neet to use IntentService? Can't I make it to something closer to previous versions of loopj?

error log:
10-22 18:44:05.525 13303-13454/br.com.maisapp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: 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:1165)
at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:868)
at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:842)
at br.com.maisapp.Helpers.MaisAppAPI.get(MaisAppAPI.java:75)
at br.com.maisapp.MainActivity.getRecentPendent(MainActivity.java:232)
at br.com.maisapp.MainActivity.access$600(MainActivity.java:55)
at br.com.maisapp.MainActivity$Iniciar.doInBackground(MainActivity.java:220)
at br.com.maisapp.MainActivity$Iniciar.doInBackground(MainActivity.java:178)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

My code runs on Main Thread:

private void getRecentPendent(){
    RequestParams params = new RequestParams();
    client.get(this, url, params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, org.apache.http.Header[] headers, JSONArray r) {
            //do something
        }

        @Override
        public void onFailure(int statusCode, org.apache.http.Header[] headers, java.lang.Throwable error, JSONObject response) {
            //do something
        }
    });
}

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

No branches or pull requests

4 participants