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;

public void onOpen(WebSocket webSocket, Response response) {
Log.d("myTag", ">>>>>>>>>>>>>>>>>>>>>>>CONNECTED<<<<<<<<<<<<<<<<<<<<<");

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

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

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

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


protected void onCreate(Bundle savedInstanceState) {
start = (Button) findViewById(;
output = (TextView) findViewById(;
client = new OkHttpClient();

start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

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


private void output(final String txt) {
runOnUiThread(new Runnable() {
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
For more options, visit