0

I'm using com.ning.http.client.AsyncHttpClient to open a web socket connection as follows.

AsyncHttpClient client = new AsyncHttpClient();
try {
    client.prepareGet(url)
         .execute(new WebSocketUpgradeHandler.Builder()
                 .addWebSocketListener(new WebSocketListener() {
                     @Override
                     public void onOpen(WebSocket webSocket) {
                         Log.d(TAG, "opened");
                     }

                     @Override
                     public void onClose(WebSocket webSocket) {
                         Log.d(TAG, "closed");
                     }

                     @Override
                     public void onError(Throwable throwable) {
                         Log.d(TAG, "error");
                     }
                 }).build());
} catch (IOException e) {
    e.printStackTrace();
}

This is outlined in the github documentation https://github.com/AsyncHttpClient/async-http-client.

However I'm seeing onError being called each time with a android.os.NetworkOnMainThreadException exception.

Why is this? Do I really need a separate thread in which the client can operate or an instance of AsyncTask?

Ken
  • 30,811
  • 34
  • 116
  • 155

1 Answers1

0

Re-reading the documentation,

Async Http Client library purpose is to allow Java applications to easily execute HTTP requests and asynchronously process the HTTP responses.

suggests that the request execution blocks but the response handling does not.

Ken
  • 30,811
  • 34
  • 116
  • 155