Java Android loopj Async Http 在 1.4.5 更新后崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24646635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android loopj Async Http crashes after 1.4.5 update
提问by Phil
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.
Android loopj Async Http lib 的新更新已经发布,并且它们发生了很大变化。现在您需要手动设置,Looper.prepare()
否则默认情况下它使用同步模式而不是异步模式。我不知道我需要在哪里设置它。
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.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.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.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:
我的 Http 请求类:
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?
谁能帮我?
回答by Leonardo
It's because this version have a several bugs.
这是因为这个版本有几个错误。
I high recomend you use OkHttp async layer, it's basically the same structure.
我强烈建议您使用 OkHttp 异步层,它的结构基本相同。
Or if you want the same structure based on OkHttpClient (Square Inc) use this:
或者,如果您想要基于 OkHttpClient (Square Inc) 的相同结构,请使用以下命令:
回答by Paul T.
I had a similar issue and found that making HTTP requests with an AsyncHttpClient in a thread caused the problem.
我有一个类似的问题,发现在线程中使用 AsyncHttpClient 发出 HTTP 请求会导致问题。
I ran my HTTP request outside of the thread and it fixed the problem for me. You can try something like:
我在线程之外运行了我的 HTTP 请求,它为我解决了这个问题。您可以尝试以下操作:
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;
}
}
回答by Tyler Davis
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
我不同意保罗的做法。虽然我真的看不出有什么好方法可以解决这个问题,因为我即将介绍的方式也相当笨拙,但不要使用 AsyncHttpResponseHandler,而是使用这个类
public abstract class AlwaysAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
@Override
public boolean getUseSynchronousMode() {
return false;
}
}
回答by Ashok
I solved the issue by passing parameter in AsyncHttpResponseHandler(Looper.getMainLooper()) like this.
我通过像这样在 AsyncHttpResponseHandler(Looper.getMainLooper()) 中传递参数解决了这个问题。
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 和 Download Class。在 MainActivity 类中,我调用了在 Downloadclass 中实现的 post 方法。然后下载类包含 POST () 这是从我的库中导入的,如 import com.loopj.android.http.*;
MainActivity.Class
主Activity.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 );
}
回答by angel
I solve it with one code line
我用一行代码解决了
I separated my responseHandler
我分离了我的 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);