3

I am testing nv-websocket-client on Android using Android Studio 2.2.3/JRE1.8.0_76.

My code is basically the same as the echo sample application in the README.md file. The class runs fine under Java 8 Update 111, but fails under Android Studio. I walked the code in debug mode and it failed at:

Source:com/neovisionaries/ws/client/Address.java:36

InetSocketAddress toInetSocketAddress()
{
    return new InetSocketAddress(mHost, mPort);// mHost = "echo.websocket.org", mPort = 80.
}

Error message in Android Studio:

Method threw 'java.lang.NullPointerException' exception. Cannot evaluate java.net.InetSocketAddress.toString()

Any idea what I did wrong here?

My Test Class:

public class TestWS {

private String m_msg;
private String m_uriStr; // = "ws://echo.websocket.org";
private static final int TIMEOUT = 5000;

public TestWS(String uriStr) {
    m_uriStr = uriStr;
    m_msg = "";
}

public void RunTest() {
    try {
        // Connect to the echo server.
        WebSocket ws = connect();
        ws.sendText("This is a test");
        Thread.sleep(1000); // Make sure m_msg is updated before function return.
        ws.disconnect();
    }
    catch(Exception ex)
    {
        m_msg += "Exception: " + ex.getMessage(); // getMessage() returns null.
    }
}

private WebSocket connect() throws IOException, WebSocketException
{
    return new WebSocketFactory()
            .setConnectionTimeout(TIMEOUT)
            .createSocket(m_uriStr)
            .addListener(new WebSocketAdapter() {
                // A text message arrived from the server.
                @Override
                public void onTextMessage(WebSocket websocket, String message) {
                    m_msg += "Echo: " + message;
                }
            })
            .addExtension(WebSocketExtension.PERMESSAGE_DEFLATE)
            .connect(); // <= failed in this function.
}

public String GetMsg(){
    return m_msg;
}
}
Chu Bun
  • 513
  • 1
  • 5
  • 17
  • I found in a comment for this question http://stackoverflow.com/questions/30547517/which-websocket-library-to-use-in-android-app that this library requires JDK8 and thus won't run on Android. Anybody knows if this is true? – Chu Bun Jan 06 '17 at 16:21
  • did you found the solution for this? in my case I'm trying to re-connect with different address, and I got error, but message is NULL, so, no clue what happens.. – Kostanos Apr 01 '19 at 20:58
  • No I didn't find any solution and had to switch to another web-socket client. – Chu Bun Apr 02 '19 at 22:26

0 Answers0