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.
在该消息之后它完全崩溃
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$3.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$1.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$2.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
我的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);
}
}
任何人都可以帮助我吗?
答案 0 :(得分:33)
我遇到了类似的问题,发现在线程中使用AsyncHttpClient发出HTTP请求会导致问题。
我在线程之外运行了我的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;
}
}
答案 1 :(得分:12)
我不同意Paul的做法。虽然我不能真正看到一种解决这个问题的好方法,因为我即将呈现的方式也是相当hacky,但不是使用AsyncHttpResponseHandler而是使用此类
public abstract class AlwaysAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
@Override
public boolean getUseSynchronousMode() {
return false;
}
}
答案 2 :(得分:5)
我用一个代码行解决它
我将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);
}
} ;
这是圣线
responseHandler.setUsePoolThread(true);
答案 3 :(得分:2)
我通过在AsyncHttpResponseHandler(Looper.getMainLooper())中传递参数解决了这个问题。
首先,我使用了两个类,一个是MainActivity和Download Class。在MainActivity类中,我调用在Downloadclass中实现的post方法。然后Download类包含从我的库导入的POST(),如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 );
}
答案 4 :(得分:-1)
这是因为这个版本有几个错误。
我高度建议您使用OkHttp异步层,它的结构基本相同。
或者如果你想要基于OkHttpClient(Square Inc)的相同结构,请使用: