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?