当前线程尚未调用Looper.prepare()。强制同步模式

时间:2016-01-19 13:11:13

标签: android multithreading

我在logcat中收到Current thread has not called Looper.prepare(). Forcing synchronous mode警告。因为我无法进入onsuccess响应处理程序。

我提到了post。但它对我没用。

堆栈跟踪:

01-19 22:52:30.683: W/AsyncHttpRH(23815): Current thread has not called Looper.prepare(). Forcing synchronous mode.

01-19 22:52:30.685: W/System.err(23815): java.lang.IllegalArgumentException: Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.
01-19 22:52:30.686: W/System.err(23815):    at com.loopj.android.http.AsyncHttpClient.sendRequest(AsyncHttpClient.java:1493)
01-19 22:52:30.686: W/System.err(23815):    at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:1078)
01-19 22:52:30.686: W/System.err(23815):    at com.loopj.android.http.AsyncHttpClient.get(AsyncHttpClient.java:1052)
01-19 22:52:30.686: W/System.err(23815):    at agriya.talkr.localdb.RestClient.get(RestClient.java:29)
01-19 22:52:30.686: W/System.err(23815):    at agriya.talkr.localdb.ServerCallee.execute(ServerCallee.java:77)
01-19 22:52:30.686: W/System.err(23815):    at agriya.talkr.CreateGroup$synchronizeInBg.doInBackground(CreateGroup.java:213)
01-19 22:52:30.686: W/System.err(23815):    at agriya.talkr.CreateGroup$synchronizeInBg.doInBackground(CreateGroup.java:1)
01-19 22:52:30.686: W/System.err(23815):    at android.os.AsyncTask$2.call(AsyncTask.java:292)
01-19 22:52:30.686: W/System.err(23815):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-19 22:52:30.686: W/System.err(23815):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-19 22:52:30.686: W/System.err(23815):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-19 22:52:30.686: W/System.err(23815):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-19 22:52:30.686: W/System.err(23815):    at java.lang.Thread.run(Thread.java:818)

下面我发布了代码并指出错误行:

CreatGroup.java:

public class CreateGroup extends AppCompatActivity {

........
........

    @Override
        protected String doInBackground(String... params) {
          try {

       .......
      ServerCallee.execute(Constants.SERVER_CALL_newGroup, map); --->213th line
    } catch (JSONException e) {

        e.printStackTrace();
        return "1";

        }

ServerCallee.java:

public static void execute(final Constants _service, HashMap<String, String> params){

  ........
  ........


 RestClient.get(null, rqm, new JsonHttpResponseHandler(){  --->77th line

 ........
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        System.out.println("onFailure - String: " + _service.toString());

        Log.e("fail 1", "Test");


     }
      .......(On Success response handler)

RestClient.java:

 public class RestClient {

          private static AsyncHttpClient client = new AsyncHttpClient();

      public static void get( String url,  RequestParams params, final JsonHttpResponseHandler responseHandler) {

              client.get(getAbsoluteUrl(url), params, responseHandler); -->29th line

          }

          public static void post(final String url, final RequestParams params, final JsonHttpResponseHandler responseHandler) {

             client.get(getAbsoluteUrl(url), params, responseHandler);

          }
    }

2 个答案:

答案 0 :(得分:5)

虽然你没有显示它,但似乎你是从AsyncTask调用它。在这种情况下,您可以使用错误消息建议的SyncHttpClient

答案 1 :(得分:0)

就我而言。 以前我在用

AsyncHttpClient client = new AsyncHttpClient();

然后我将其更改为:

SyncHttpClient client = new SyncHttpClient();

client.get(CommentActivity.this, url, params , new JsonHttpResponseHandler()......