简体   繁体   中英

Display message in Textview when Websocket message recieved

this is my websocket client in android. It's a quick test and the reply from server should be display on the TextView .

Boolean status1 = false;
static String msg = "reply ";   


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
    java.lang.System.setProperty("java.net.preferIPv4Stack", "true");

    final TextView text = (TextView) findViewById(R.id.textView1);
    final TextView text1 = (TextView) findViewById(R.id.textView2);
    final Button button = (Button) findViewById(R.id.button1);


    try {
        final WebSocketClient client = new WebSocketClient(new URI(
                "ws://10.0.2.2:8080/_024_WebSocketTest4/echo")) {

            @Override
            public void onOpen(ServerHandshake handshakedata) {
                Log.d("1", "open");
                status1 = true;

            }

            @Override
            public void onMessage(String message) {

                msg += message;
                Log.d("reply", message);

                print(text1);

            }

            @Override
            public void onError(Exception ex) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onClose(int code, String reason, boolean remote) {
                status1 = false;

            }
        };

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                connect(client);


            }
        });




    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


public void connect(WebSocketClient client) {

    final TextView text = (TextView) findViewById(R.id.textView1);
    client.connect();

    while (!status1) {

    }

    text.setText(client.getReadyState().toString());
    client.send("Hello");
    //final TextView text1 = (TextView) findViewById(R.id.textView2);

    //text1.setText(msg);
}

public void print(TextView text1) {

    Log.d("print", msg);
    text1.setText(msg);
}

When server send the message it is not showing in the textview. But in Logcat the reply is displayed. What could be the problem?

You probably need to update the TextView from the main thread.

Something along the lines of..

Handler mHandler = new Handler(); 
// ...
// onMessage:
mHandler.post(new Runnable() {
    public void run() {
        //.. update UI
    }
};

If you are going to update your TextView in another thread then you should do something below

textView.post(new Runnable() {
    public void run() {
        textView.setText(yourText);
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM