Android – IOException when using loopj synchttpclient

I need to use loopj's synchttpclient in several areas. When I use asynchttpclient, the request returns successfully. When I use synchttpclient, the answer accepted here is as follows: how to use loopj synchttpclient for synchronous calls?, I encountered a breakpoint in onfailure. Statuscode is 0, errorresponse is null, throwable is java.io.ioexception: unhandled exception: null

This is the relevant code. When I use async again, the effect is very good:

        buttonTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                AsyncHttpClient httpClient = new AsyncHttpClient();
                SyncHttpClient httpClient = new SyncHttpClient();

                httpClient.get("http://10.0.1.6:3000/home/test_endpoint", new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        String stringResponse = response.toString();
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                        String error = errorResponse.toString();
                    }
                });

                String temp = "got here";
            }
        });

I'm compiling 'com. Loopj. Android: Android async http: 1.4.9' using

resolvent:

When I clicked this, I did some debugging and found that loopj was eating the following exceptions:

android.os.networkonmainthreadException

I can't provide a detailed stack trace because the exception doesn't seem to be logged correctly (for some reason, logcat didn't extract it at all)

However, it is located here:

AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:203)

I've been running my code:

Handler handler = new Handler();
Runnable r = new Runnable(){
  public void run(){
    SyncHttpClient client ....
    client.get(.....); // hit error here
  }
};

As you can see, I happen to run my synchttpclient in the handler. Post () method, but obviously the implementation does not count as (a network on a non main thread)

The key to fixing it is to use the "new thread (...)" contained in the code of Mike orr85. Like this

new Thread(Runnable r = new Runnable(){
  public void run(){
    SyncHttpClient client ....
    client.get(.....); // hit error here
  }
}).start();

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>