0

我正在使用 socketIO 库开发一个聊天应用程序。除了 UI 更新之外,一切似乎都运行良好。我能够向端口发送消息并从中接收。但收到后,我无法更新 textview 或任何其他 UI。代码如下

非常感谢任何帮助

注意:祝酒词来了,**更新的*消息也会在下一条新消息到来时显示

聊天室活动.java

public class chatRoomActivity extends Activity implements OnClickListener{

int channelId = 0;
public final String SocketURL = "someURL";
String userName = "";
int connectioAttempt = 0;

SocketIOClient socketClient = null;
SocketIOClientOperations sioOps = null;

Button chatRoomSend = null;
EditText chatInput = null;
TextView msgcntr;


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.chat_room);

    chatRoomSend = (Button)findViewById(R.id.chatRoomSendBtn);
    chatInput = (EditText)findViewById(R.id.chatRoomInput);
    msgcntr=(TextView)findViewById(R.id.textView1);
    chatRoomSend.setOnClickListener(this);


        sioOps = new SocketIOClientOperations();

            msgcntr = (TextView) findViewById(R.id.textView1);

            socketClient = sioOps.connectToSocket(SocketURL);

            System.out.println("**********Connected**********");

            if(socketClient != null)
            {
                boolean joined = false;

                joined = sioOps.joinChannel(socketClient, channelId);
                System.out.println("*******Joined*********");
            }

            else
            {
                System.out.println("*******Null*********");
            }

            socketClient.on("incomingMessage", new EventCallback() {


                public void onEvent(JSONArray argument, Acknowledge acknowledge) {
                    System.out.println("***************New Message*********");

                    System.out.println("***************"+argument.toString()+"*********");
                    msgcntr.setText("test");
                    updateClock();

                }

            });


}


@Override
public void onClick(View v) {

    if(v == chatRoomSend)
    {
        boolean sent = sioOps.sendNewMessage(socketClient, userName, "empty");
        if(sent)
            System.out.println("************** Message Sent *********");
        else
            System.out.println("************** Message Failed *********");
    }

}

protected void updateClock() {
    runOnUiThread(new Runnable(){
        public void run() {
            Toast.makeText(getApplicationContext(), "end", Toast.LENGTH_SHORT).show();
            msgcntr.setText("test");
            System.out.print("************Updated*********");
        }
     });


}

}

socketIOClientOperations.java

public class SocketIOClientOperations {

public SocketIOClientOperations(){}

public SocketIOClient connectToSocket(String SocketURL)
{
    SocketIORequest connectionRequest = new SocketIORequest(SocketURL);
    SocketIOClient socketClient = null;
    try {
        socketClient = SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), connectionRequest, null).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
    return socketClient;
}

public boolean joinChannel(SocketIOClient client,int channelId)
{
    JSONArray joinChannel = new JSONArray();
    JSONObject values = new JSONObject();
    try {

        values.put("channelID", channelId);
        joinChannel.put(values);

        client.emit("joinChannel", joinChannel);
        return true;

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

}

public boolean sendNewMessage(SocketIOClient client,String name,String txt)
{
    JSONArray newMsg = new JSONArray();
    JSONObject msgBody = new JSONObject();
    try {

        msgBody.put("From", name);
        msgBody.put("Content", txt);
        newMsg.put(msgBody);

        client.emit("newMessage", newMsg);
        System.out.println("***************SendMessage*********");
        System.out.println("************** "+newMsg.toString()+" *********");
        return true;

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

}

聊天室.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:background="#000"
    android:padding="10dp"
    android:id="@+id/header">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chat Room"
        android:textColor="#fff"
        android:textSize="14sp"/>

</RelativeLayout>

<ListView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:stackFromBottom="true"
    android:layout_above="@+id/footer"
    android:layout_below="@+id/header"
    android:id="@+id/chatRoomMsgs"
    android:background="#cccccc"
    android:visibility="gone" />

 <TextView
     android:id="@+id/msgConatainer"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_above="@+id/fooer"
     android:layout_below="@+id/header"
     android:background="#ccc"
     android:textColor="#000" />

<RelativeLayout 
    android:id="@+id/fooer"
    android:background="#000"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:layout_alignParentBottom="true">

    <EditText 
        android:id="@+id/chatRoomInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your message...."
        android:layout_toLeftOf="@+id/chatRoomSendBtn"
        android:layout_alignParentLeft="true"/>

    <Button
        android:id="@+id/chatRoomSendBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Send"
        android:layout_alignParentRight="true" />

</RelativeLayout>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/msgConatainer"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="48dp"
    android:text="TextView"
    android:textColor="@android:color/black" />

</RelativeLayout>
4

1 回答 1

0

试试这个方法

private Handler mHandler = new Handler();

mHandler.post(new Runnable() {
                    public void run() {
                        msgcntr.setText("test");
                    }
                });
于 2013-08-30T04:56:02.390 回答