Expected HTTP 101 response but was '200 OK' when trying to connect echo/websocket

Viewed 1414

I am just working on an android application written in native Java. I am using a server in this application so I am working with WebSockets. I am getting this error

java.net.ProtocolException: Expected HTTP 101 response but was '200 OK'

while trying to connect to ws://echo.websocket.org and I don't know why. I am new to working with WebSockets so I am really lost on that one.

That's my activity where I try to establish the connection. The important part is in InitiateSocketConnection() function

    package com.example.rockpaperscissors;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

public class Loby extends AppCompatActivity {


private String name;
private WebSocket webSocket;
private String Server_Path = "ws://echo.websocket.org";
private RecyclerView recyclerView;
private PlayersAdapter playersAdapter;


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

    name = getIntent().getStringExtra("name");
    InitiateSocketConnection();
    
}

private void InitiateSocketConnection() {

    OkHttpClient client = new OkHttpClient().newBuilder().build();
    Request request = new Request.Builder().url(Server_Path).build();
    webSocket = client.newWebSocket(request,new Socket_Listener());

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

private class Socket_Listener extends WebSocketListener
{
    @Override
    public void onOpen(WebSocket webSocket, Response response) {
        super.onOpen(webSocket, response);
        runOnUiThread(()->
        {
            Toast.makeText(Loby.this,"Socket connected successfully!",Toast.LENGTH_LONG).show();
            initializeView();
        });
    }

    @Override
    public void onMessage(WebSocket webSocket, String text) {
        super.onMessage(webSocket, text);
    }

    @Override
    public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
        super.onFailure(webSocket, t, response);
        runOnUiThread(()->
        {
            Throwable err = t;
            Toast.makeText(Loby.this, "Socket was not connected!", Toast.LENGTH_LONG).show();
        });
    }
}

private void initializeView() {
    recyclerView = findViewById(R.id.name_list);
    playersAdapter = new PlayersAdapter(getLayoutInflater());
    recyclerView.setAdapter(playersAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("name",name);
        playersAdapter.addItem(jsonObject);

        webSocket.send(jsonObject.toString());

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

I don't think that's the issue but I permitted internet in manifest and this is my relevant implantation on the build.Gradle:

 implementation 'com.squareup.okhttp3:okhttp:3.10.0'

Thanks!

1 Answers

I had the same issue when using ws://echo.websocket.org. Changing it to ws://websocket-echo.com solved my problem.

Related