I try to use AndroidAsync library. Here is a code sample:
AsyncHttpClient.getDefaultInstance()
.websocket(url, null, new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
Log.i(LOG_TAG, "onCompleted");
if (ex != null) {
Log.e(LOG_TAG, "ex != null");
Log.e(LOG_TAG, ex.toString());
ex.printStackTrace();
return;
}
if (webSocket == null) {
Log.e(LOG_TAG, "1 webSocket == null");
} else {
Log.w(LOG_TAG, "1 webSocket != null");
}
webSocket.setStringCallback(new WebSocket.StringCallback() {
public void onStringAvailable(String s) {
Log.i(LOG_TAG, "setStringCallback");
System.out.println("I got a string: " + s);
}
});'
In my case ex == null but webSocket == null. The nullpointerexception is caught inside the library code. But can I use this socket? It looks strange that it is null even when there is no exception in the callback.
If the server you're connecting to rejects the connection with a response code (HTTP style ie. 405). Then the server is essentially closing the connection during the attempted connect.
This is does not throw an exception because there was no Timeout, or Network Issue. Furthermore, there is no websocket returned because the server did not allow the connection to be established.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With