Use output from onMessage (OkHttp)?

Viewed 155

I'm fetching data from WebSocket with OkHttp, and then I'm displaying it on an Android emulator with runOnUiThread (MainActivity). There are multiple server responses (events).

The onMessage function of my WebSocketListener looks like that:

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

So that's how I get special events displayed as a text response.

Is there any possibility to actually get this data as some new object or maybe global variable, in order to use it anywhere else or to pass it to some other activity?

Let's say - using OkHttp, the socket and its listener, what else should I do to be able to use/save/pass the data which comes from the server as just a text?

What exactly is happening in onMessage? Do we just read?

1 Answers

what else should I do to be able to use/save/pass the data which comes from the server as just a text

You can create a BlockingQueue before initializing server code. Then you can add the text from server into this queue from the onMessage function, and have a separate thread reading from this queue. This essentially needs some concurrent programming.

What exactly is happening in onMessage? Do we just read?

Since this is a callback function, and is not called by any user-code, so we cannot really return any value (return control to where?), and hence it is a 'public void' function.

Related