2013-08-30 90 views
0

我正在开发一个使用socketIO库的聊天应用程序。除了UI更新以外,每件事似乎都很好。我能够发送消息到端口并从中接收。但接待后,我不能更新textview或任何其他用户界面。下面TextView不更新

代码给出任何帮助表示最欣赏

注:敬酒来了,当时下一个新的消息来

chatRoomActivity.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*********"); 
     } 
    }); 


} 

}

socketIO ClientOperations.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; 
    } 
} 

} 

chat_room.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> 

回答

0

尝试这种方式

private Handler mHandler = new Handler(); 

mHandler.post(new Runnable() { 
        public void run() { 
         msgcntr.setText("test"); 
        } 
       }); 
+0

我要试试这个,会更新你的结果。谢谢 –

+0

它不工作。你有什么想法为什么发生? –

+0

它给出了任何错误? –