Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

koush AndroidAsync webSocket nullpointerexception

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.

like image 553
Sergey Novikov Avatar asked Mar 24 '26 17:03

Sergey Novikov


1 Answers

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.

like image 152
bleuf1shi Avatar answered Mar 26 '26 08:03

bleuf1shi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!