0

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)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Where is your web socket server running? And where your client? – greenapps Oct 30 '17 at 21:21
  • I am trying to connect to localhost via the websocket. I have implemented a myHandler Class. But I don't understand where my problem is ... Could you please explain your question more? – GreatDanton Oct 30 '17 at 21:25
  • What do you consider to be 'localhost'? Further you did not tell where your client is running. – greenapps Oct 30 '17 at 21:26
  • I am a beginnger, so I might not be understanding, what you mean exactly. I wrote those classes in android studio and the server class in intellij. This is all on my local machine. – GreatDanton Oct 30 '17 at 21:28
  • You should tell me where your server program is running. And where your client app is running. Looks a basic question to me. Is there an Android device in play? – greenapps Oct 30 '17 at 21:30
  • They are both running on my local machine ... I am using an emulator in android studio – GreatDanton Oct 30 '17 at 21:31
  • So the server is running on your pc. And the client runs in an emulator on the same pc. Well then you cannot use 127.0.0.1 as that is an address you would use if the server ran on the same device as your client. In your setup that would be if the server would run on the emulator too. – greenapps Oct 30 '17 at 21:34
  • Use 10.0.2.2 to connect an app on an emulator with a server on the pc. – greenapps Oct 30 '17 at 21:35
  • OK, so what do you suggest? – GreatDanton Oct 30 '17 at 21:36
  • I used 10.0.2.2 instead, but it crashes too for the same reason. – GreatDanton Oct 30 '17 at 21:37
  • Well can very good be as your code looks impossible. It starts already with the .connect() statement. How can that be executed on the main thread? For a network call you should put that code in a thread or asynctask. Dit you check if there is a connection after you called .connect? See the server code/log to find out. – greenapps Oct 30 '17 at 21:40
  • But when I used, what you told me, I got this in intellij: Handshake failed due to unsupported WebSocket version: 8. Sup – GreatDanton Oct 30 '17 at 21:41
  • Please comment on that yourself first. – greenapps Oct 30 '17 at 21:44
  • What do you mean? :D – GreatDanton Oct 30 '17 at 21:47
  • Why did you tell about the unsupported version? Without a question? You can try to answer that question first instead of just dumping error messages. – greenapps Oct 30 '17 at 21:48
  • I thought, it might be the cause of the problem, or it might help solving the problem. I don't really know ... I have trying so much ... and without solution :( – GreatDanton Oct 30 '17 at 21:50
  • I saw other codes where the connect method was call in the on create of the main thread ... But I don't understand, why it doesn't work with me. – GreatDanton Oct 30 '17 at 21:51
  • Yes it might be the cause indeed. But if version 8 is unsupported then which version is supported?. You did not tell anything about it. You did not tell about versions at all. You did not comment. I am still waiting for you to comment. – greenapps Oct 30 '17 at 21:53
  • My problem, is that I don't know, how to know the supported websocket version – GreatDanton Oct 30 '17 at 21:58
  • Does not matter. And not to the point. I wanted to hear from you: Oh.. the software version of my clients websocket is different from that of the servers websocket. – greenapps Oct 30 '17 at 22:01
  • OK ... The question is how to solve this ... :-) – GreatDanton Oct 30 '17 at 22:10
  • Indeed. But if the software versions are different then i would have expected you to tell something about your server software. You told nothing about your server. And... Try to get equal software versions i would say. – greenapps Oct 30 '17 at 22:13

0 Answers0