Android Loopj Async Http Crashes After 1.4.5 Update
Solution 1:
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:
publicclassHttpRequest {
// A SyncHttpClient is an AsyncHttpClientpublicstatic AsyncHttpClient syncHttpClient= new SyncHttpClient();
publicstatic AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
publicstaticvoidsetCookieStore(PersistentCookieStore cookieStore) {
getClient().setCookieStore(cookieStore);
}
publicstaticvoidget(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
getClient().get(url, params, responseHandler);
}
publicstaticvoidpost(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.
*/privatestatic AsyncHttpClient getClient()
{
// Return the synchronous HTTP client when the thread is not preparedif (Looper.myLooper() == null)
return syncHttpClient;
return asyncHttpClient;
}
}
Solution 2:
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
publicabstractclassAlwaysAsyncHttpResponseHandlerextendsAsyncHttpResponseHandler {
@OverridepublicbooleangetUseSynchronousMode() {
returnfalse;
}
}
Solution 3:
I solve it with one code line
I separated my responseHandler
JsonHttpResponseHandlerresponseHandler=newJsonHttpResponseHandler(){
@OverridepublicvoidonSuccess(int statusCode, Header[] headers, JSONObject response) {
RecorridoResponseDTO respuesta= newGson().fromJson(response.toString(), RecorridoResponseDTO.class);
recorrido.setRecorridoId(respuesta.getA());
mDataManager.actualizarRecorrido(recorrido);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@OverridepublicvoidonFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
}
} ;
and this was the holy line
responseHandler.setUsePoolThread(true);
Solution 4:
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
publicvoidonSuccess(int statusCode,org.apache.http.Header[] headers,byte[] responseBody) {
System.out.println(" Success ="+responseBody);
}
@Override
publicvoidonFailure(int statusCode,org.apache.http.Header[] headers,byte[] responseBody, Throwable error) {
System.out.println( "Failure");
}
});
DownLoad.Class
AsyncHttpClient asynClient = newAsyncHttpClient();
voidpost(Context context,String url, StringEntity entity,Stringstring, AsyncHttpResponseHandler asyncHttpResponseHandler) {
asynClient.addHeader("Accept", "application/json");
asynClient.addHeader("Content-type", "application/json");
asynClient.post(context, url, entity, "application/json", asyncHttpResponseHandler );
}
Solution 5:
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:
Post a Comment for "Android Loopj Async Http Crashes After 1.4.5 Update"