Android loopj Async Http crashes after 1.4.5 update

安稳与你 提交于 2019-12-02 22:53:12
Paul T.

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

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!