Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOException when using loopj SyncHttpClient

I have the need to use loopj's SyncHttpClient in a couple areas. When I use AsyncHttpClient, the request returns successfully. When I use the SyncHttpClient as shown in the accepted answer here: How to use loopJ SyncHttpClient for synchronous calls?, I hit a breakpoint in onFailure. The statusCode is 0, the errorResponse is null, and throwable is java.io.IOException: Unhandled exception: null.

Here is the relevant code. Again when I use Async it works fine:

        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 using compile 'com.loopj.android:android-async-http:1.4.9'

like image 607
mikeorr85 Avatar asked Sep 25 '22 11:09

mikeorr85


2 Answers

I did some debugging when I hit this, and I discovered that loopj is eating the following exception:

android.os.NetworkOnMainThreadException

I can't give a detailed stack trace because this exception doesn't seem to be logged properly (logcat is not picking it up at all for some reason)

However, it's located here:

AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:203)

I have been running my code like this:

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 be running my SyncHttpClient within a Handler.post() method, but apparently that implementation does not count as (Networking on a non-main thread)

The key thing that fixed it was the use of "new Thread(...)" that mikeorr85's code included. Like so ...

new Thread(Runnable r = new Runnable(){
  public void run(){
    SyncHttpClient client ....
    client.get(.....); // hit error here
  }
}).start();
like image 107
Furyvore Avatar answered Oct 06 '22 02:10

Furyvore


You must still run it off the main thread. This is the code that worked for me and allowed me to hit a breakpoint set at - "String temp = 'got here'" after the request completed:

        buttonTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        AsyncHttpClient httpClient = new AsyncHttpClient();
                        SyncHttpClient httpClient = new SyncHttpClient();

                        httpClient.get("http://10.0.1.6:3000/home/test_endpoint", null, 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";
                    }
                }).start();
            }

More code is available on their github page: https://github.com/loopj/android-async-http/blob/master/sample/src/main/java/com/loopj/android/http/sample/SynchronousClientSample.java

like image 26
mikeorr85 Avatar answered Oct 06 '22 03:10

mikeorr85