5

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'

3
  • Confirm that the server is actually accessible. Do you have network permissions? Commented Feb 7, 2016 at 4:58
  • add the following line inside onFailure throwable.printstacktrace(); and display logcat in here. Commented Feb 7, 2016 at 6:43
  • I am also getting the same issue. Commented Sep 26, 2017 at 8:34

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

1 Comment

How would one block then? The whole point of the SyncClient is that its not async. I want to make a network call and block until it is done?
1

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

Comments

-1

I use AsyncHttpClient and it work for me fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.