16

The new update for Android loopj Async Http lib is out and they changed a lot. Now you need to manually set Looper.prepare() otherwise it uses synchronious mode instead of async by default. I don't get where I need to set it.

Logcat

07-09 08:16:18.775: W/AsyncHttpResponseHandler(6606): Current thread has not called Looper.prepare(). Forcing synchronous mode.

After that message it totally crashes

07-09 08:16:18.835: E/AndroidRuntime(6606): FATAL EXCEPTION: AsyncTask #1
07-09 08:16:18.835: E/AndroidRuntime(6606): java.lang.RuntimeException: An error occured while executing doInBackground()
07-09 08:16:18.835: E/AndroidRuntime(6606):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.lang.Thread.run(Thread.java:864)
07-09 08:16:18.835: E/AndroidRuntime(6606): Caused by: java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1096)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:873)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:856)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.loopj.android.http.AsyncHttpClient.post(AsyncHttpClient.java:843)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.xxx.app.HttpRequestGCM.post(HttpRequestGCM.java:15)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.xxx.app.ChatActivity$RegisterBackground.sendRegistrationIdToBackend(ChatActivity.java:681)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.xxx.app.ChatActivity$RegisterBackground.doInBackground(ChatActivity.java:660)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at com.xxx.app.ChatActivity$RegisterBackground.doInBackground(ChatActivity.java:1)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-09 08:16:18.835: E/AndroidRuntime(6606):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-09 08:16:18.835: E/AndroidRuntime(6606):     ... 5 more

My class for the Http Request:

import android.os.Looper;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class HttpRequest {
      public static AsyncHttpClient client = new AsyncHttpClient();

      public static void setCookieStore(PersistentCookieStore cookieStore) {
            client.setCookieStore(cookieStore);
        }

      public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
          Looper.prepare();
          client.get(url, params, responseHandler);
      }

      public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
          Looper.prepare();
          client.post(url, params, responseHandler);
      }
}

Can anyone help me?

Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
Phil
  • 583
  • 4
  • 7
  • 19

5 Answers5

33

I had a similar issue and found that making HTTP requests with an AsyncHttpClient in a thread caused the problem.

I ran my HTTP request outside of the thread and it fixed the problem for me. You can try something like:

public class HttpRequest {

  // A SyncHttpClient is an AsyncHttpClient
  public static AsyncHttpClient syncHttpClient= new SyncHttpClient();
  public static AsyncHttpClient asyncHttpClient = new AsyncHttpClient();

  public static void setCookieStore(PersistentCookieStore cookieStore) {
      getClient().setCookieStore(cookieStore);
  }

  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      getClient().get(url, params, responseHandler);
  }

  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
      getClient().post(url, params, responseHandler);
  }

  /**
   * @return an async client when calling from the main thread, otherwise a sync client.
   */
  private static AsyncHttpClient getClient()
  {
      // Return the synchronous HTTP client when the thread is not prepared
      if (Looper.myLooper() == null)
          return syncHttpClient;
      return asyncHttpClient;
  }
}
AshesToAshes
  • 937
  • 3
  • 14
  • 31
Paul T.
  • 705
  • 6
  • 14
  • but how to properly prepare the looper and where to set i guess we want to use asynchronous mode – Ravi Aug 28 '14 at 10:45
  • 1
    What if I dont want to use SyncHttpClient? – Lucas Jota Oct 22 '14 at 20:13
  • Ravi You could either call Looper.prepare() or you could wrap your HTTP request call in "runOnUIThread" depending on where you want to make the call from. @Lucas If you don't want to use the SyncHttpClient, then you can make your HTTP requests outside of threads/AsyncTasks. – Paul T. Feb 24 '15 at 21:02
12

I disagree with doing it Paul's way. Although I can't really see a good way to get around this as the way I'm about to present is fairly hacky as well, but instead of using AsyncHttpResponseHandler use this class instead

public abstract class AlwaysAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
    @Override
    public boolean getUseSynchronousMode() {
        return false;
    }
}
Tyler Davis
  • 2,420
  • 2
  • 23
  • 19
  • This solution worked for me. I was trying to initiate a Async request from a thread (that would die soon). I have other synchronous calls in this thread, but for this call I wanted asynchronous and it was giving me an exception until I overridden the mentioned method. Thanks Tyler. – Rodrigo Jan 27 '15 at 23:26
6

I solve it with one code line

I separated my responseHandler

JsonHttpResponseHandler responseHandler = new JsonHttpResponseHandler(){


            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                RecorridoResponseDTO respuesta= new Gson().fromJson(response.toString(), RecorridoResponseDTO.class);
                recorrido.setRecorridoId(respuesta.getA());
                mDataManager.actualizarRecorrido(recorrido);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }


            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable, errorResponse);
            }


        } ;

and this was the holy line

responseHandler.setUsePoolThread(true);
angel
  • 4,474
  • 12
  • 57
  • 89
3

I solved the issue by passing parameter in AsyncHttpResponseHandler(Looper.getMainLooper()) like this.

First of all i used two classes one is MainActivity and Download Class. In MainActivity class, i call post method which is implement in the Downloadclass. Then Download class contains POST () which was import from my library like import com.loopj.android.http.*;

MainActivity.Class

clientObj.post(context,url,entity, "application/json", new AsyncHttpResponseHandler(Looper.getMainLooper()) {

@Override
public void onSuccess(int statusCode,org.apache.http.Header[] headers,byte[] responseBody) {
        System.out.println(" Success ="+responseBody);
}
@Override
public void onFailure(int statusCode,org.apache.http.Header[] headers,byte[] responseBody, Throwable error) {
    System.out.println( "Failure");
}
});

DownLoad.Class

    AsyncHttpClient asynClient = new AsyncHttpClient();


void post(Context context,String url, StringEntity entity,String string, AsyncHttpResponseHandler asyncHttpResponseHandler) {
    asynClient.addHeader("Accept", "application/json");
    asynClient.addHeader("Content-type", "application/json");
    asynClient.post(context, url, entity, "application/json", asyncHttpResponseHandler );
}
Ashok
  • 839
  • 10
  • 21
-1

It's because this version have a several bugs.

I high recomend you use OkHttp async layer, it's basically the same structure.

Or if you want the same structure based on OkHttpClient (Square Inc) use this:

https://github.com/leonardoxh/AsyncOkHttpClient

Leonardo
  • 155
  • 3
  • 3
    I just found the problem. It's because of the registration process of GCM. I just changed Async so Sync because this process is actually in an AsyncTask so it doesn't matter if it's Sync – Phil Jul 10 '14 at 14:19
  • Since 1 year after the comment ? I think you are mad. – Leonardo Apr 09 '15 at 11:39
  • I downvoted because this answer is not at all informative. "You have several bugs. You should switch frameworks." The first seems obvious, the second non sequitur. – Kristopher Micinski Mar 21 '16 at 19:31