0

Supposet my websocket server sends data to my android client which is defined as given below

public class ClientConnection extends AsyncTask<Void, Void, Void> {

    private void connectWebSocket() {
        URI uri;

        mWebSocketClient = new WebSocketClient(uri) {

            @Override
            public void onOpen(ServerHandshake serverHandshake) {
            }

            @Override
            public void onMessage(String s) {
                                DO_MY_UI_CHANGES(s);
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                Log.d("Websocket", "Closed " + s);
            }

            @Override
            public void onError(Exception e) {
                Log.d("Websocket", "Error " + e.getMessage());
            }
        };
        mWebSocketClient.connect();
    }

Now let say I want to Update my main threads UI element after receiving data from server. As of now I know that DO_MY_UI_CHANGES(s) wont work because we are trying to modify main threads element in this thread which isnt allowed in android.

Please suggest me something so that I can make call to DO_MY_UI_CHANGES.

8
  • Start by reading the documentation developer.android.com/reference/android/os/AsyncTask.html It talks about exactly what you're trying to do... specifically the onPostExecute method. Commented Apr 6, 2014 at 7:29
  • Look if I call .execute() method then surely whatever I wrote in onPostExecute will get reflected on the UI. But what if I got message from server, then in that case onMessage will fire. Can you elaborate what will happen next Commented Apr 6, 2014 at 8:40
  • When your message comes back from the server, it goes into onPostExecute... what happens after that is in your hands. Commented Apr 6, 2014 at 8:41
  • ohh, then I must recheck my code. If you can check plz just have look at my code here "stackoverflow.com/questions/22887542/…" if there's something wrong i am doing then please comment Commented Apr 6, 2014 at 9:01
  • @JeremyMiller onPostExecute method is not executing after receiving message from server. I checked that by putting log in onMessage and onPostExecute. Commented Apr 6, 2014 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.