IOException when using loopj SyncHttpClient

I need to use loopj SyncHttpClient in pairs. When I use AsyncHttpClient, the request returns successfully. When I use SyncHttpClient, as shown in the accepted answer here: How to use loopJ SyncHttpClient for synchronous calls? I hit a breakpoint at onFailure. statusCode is 0, 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 use compile 'com.loopj.android:android-async-http:1.4.9'

+4
source share
3

, , , loopj :

android.os.NetworkOnMainThreadException

, , , (logcat - )

:

AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:203)

:

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

, SyncHttpClient Handler.post(), , , (Networking on non-main thread)

, , " (...)", mikeorr85. ...

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

. , , "String temp = 'got here" :

        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();
            }

github: https://github.com/loopj/android-async-http/blob/master/sample/src/main/java/com/loopj/android/http/sample/SynchronousClientSample.java

+1

I use AsyncHttpClient and it works well for me.

0
source

Source: https://habr.com/ru/post/1627619/


All Articles