Android websocket

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Android websocket

Isuru Sahan
Hi I am new to Android..
I need to create Android websocket to my 'localhost:8080/call'

This is my class but i can't connect to the localhost... 
websocket is created on java spring project

public class MainActivity extends AppCompatActivity {

private Button start;
private TextView output;
private OkHttpClient client;

private final class EchoWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;

@Override
public void onOpen(WebSocket webSocket, Response response) {
System.out.print(">>>>>Connected<<<<<<<");
Log.d("myTag", ">>>>>>>>>>>>>>>>>>>>>>>CONNECTED<<<<<<<<<<<<<<<<<<<<<");
output("Connected");
}

@Override
public void onMessage(WebSocket webSocket, String text) {
output("Receiving : " + text);
}

@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
output("Receiving bytes : " + bytes.hex());
}

@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(NORMAL_CLOSURE_STATUS, null);
output("Closing : " + code + " / " + reason);
}

@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
output(">>>>>Not Connected<<<<<" + t.getMessage() + "Response" + response);
Log.e("Error",">>>>>>>>>NOT CONNECTED<<<<<<<<<<"+t.getMessage());
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
output = (TextView) findViewById(R.id.output);
client = new OkHttpClient();

start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
start();
}
});
}

private void start() {
//Request request = new Request.Builder().url("ws://172.17.0.1:8080/call").build();
Request request = new Request.Builder().url("ws://echo.websocket.org").build();
EchoWebSocketListener listener = new EchoWebSocketListener();
WebSocket ws = client.newWebSocket(request, listener);

client.dispatcher().executorService().shutdown();
}

private void output(final String txt) {
runOnUiThread(new Runnable() {
@Override
public void run() {
output.setText(output.getText().toString() + "\n\n" + txt);
}
});
}

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/d/optout.