Android loopj Async Http crashes after 1.4.5 update

前端 未结 5 2010
谎友^
谎友^ 2021-02-04 05:35

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 ins

5条回答
  •  别跟我提以往
    2021-02-04 05:53

    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;
      }
    }
    

提交回复
热议问题