Simple Chat Application with WebSocketClient

515 Views Asked by At

I am trying to learn how to make a connection via a websocket in android. I am trying to use the websocket client to send a message to server and then try to view it on the screen.

I can start the application, but when I type a message and then click the send Button I receive the following error: org.java_websocket.exceptions.WebsocketNotConnectedException

Here is my MainActivity Class:

public class MainActivity extends AppCompatActivity {

private TextView userView;
private ListView listView;
private EditText textToSend;
private Button sendButton;

private WebClient webClient;
private JSONObject jsonObject;

private URI uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    userView = (TextView) findViewById(R.id.userView);
    listView = (ListView) findViewById(R.id.listView);
    textToSend = (EditText) findViewById(R.id.textToSend);
    sendButton = (Button) findViewById(R.id.sendButton);

    try {
        uri = new URI("http://127.0.0.1:8080/chat");
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    webClient = new WebClient(uri);
    webClient.connect();

    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            jsonObject = new JSONObject();
            try {
                jsonObject.put("message",textToSend.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

            webClient.send(jsonObject.toString());


        }
    });
}
}

And here is the WebClient Class:

public class WebClient extends WebSocketClient {

public WebClient(URI serverURI) {
    super(serverURI);
}

@Override
public void onOpen(ServerHandshake handshakedata) {

    Log.e("Websocket", "Opened");

}

@Override
public void onMessage(String message) {



}

@Override
public void onClose(int code, String reason, boolean remote) {

}

@Override
public void onError(Exception ex) {

}
}

And this is logcat after clicking the send button:

--------- beginning of crash 10-30 20:09:50.830 2682-2682/example.com.chatapp E/AndroidRuntime: FATAL EXCEPTION: main Process: example.com.chatapp, PID: 2682 org.java_websocket.exceptions.WebsocketNotConnectedException at org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:566) at org.java_websocket.WebSocketImpl.send(WebSocketImpl.java:543) at org.java_websocket.client.WebSocketClient.send(WebSocketClient.java:171) at ziad.example.com.chatapp.MainActivity$1.onClick(MainActivity.java:60) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

0

There are 0 best solutions below