Android loopj Async Http crashes after 1.4.5 update

前端 未结 5 2008
谎友^
谎友^ 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;
      }
    }
    
    0 讨论(0)
  • 2021-02-04 05:54

    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 );
    }
    
    0 讨论(0)
  • 2021-02-04 06:02

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-04 06:05

    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

    0 讨论(0)
  • 2021-02-04 06:14

    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);
    
    0 讨论(0)
提交回复
热议问题