2015-04-07 95 views
0

我正在開發android應用程序,其中服務器將命令發送到客戶端和客戶端的命令併發送結果。值得烤,但沒有得到在TextView?

我在客戶端每15秒使用處理程序在服務器端寫入數據。

我的問題是我收到來自客戶端的輸出,並正確地烤,但價值被分配當第二次價值在於它得到正確舉杯的TextView它來自客戶端第一次,但沒有分配給TextView的

這裏我的代碼去

服務器端

public class ServerSocketNew extends ActionBarActivity { 
static final int SocketServerPORT = 8080; 
LinearLayout chatpanel, sendpanel; 
TextView infoIp, infoPort, chatMsg,contact,contact1; 
Button b1,b2,back,Files; 
EditText e1,e2; 
    private File root; 
    private ArrayList<File> fileList = new ArrayList<File>(); 
String msgLog = ""; 
String prev=""; 
String Message="Hies"; 
String Flags="Trues"; 
List<ChatClient> userList; 
    int count=0; 
ServerSocket serverSocket; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_server_socket_new); 
    infoIp = (TextView) findViewById(R.id.infoip); 
    infoPort = (TextView) findViewById(R.id.infoport); 
    chatMsg = (TextView) findViewById(R.id.chatmsg); 
    b1 = (Button) findViewById(R.id.send); 
    b2 = (Button) findViewById(R.id.Contacts); 
    e1 = (EditText) findViewById(R.id.say); 
    e2 = (EditText) findViewById(R.id.Something); 
    contact=(TextView) findViewById(R.id.contact); 
    contact1=(TextView) findViewById(R.id.contact1); 
    infoIp.setText(getIpAddress()); 
    chatpanel = (LinearLayout)findViewById(R.id.chatpanel); 
    sendpanel = (LinearLayout)findViewById(R.id.sendpanel); 
    back=(Button) findViewById(R.id.back); 
    Files=(Button) findViewById(R.id.Files); 
    OnClickListener listener=new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Message=e1.getText().toString(); 
     Flags="Trues"; 
     count=0; 

    } 
}; 
    OnClickListener Listnerfiles=new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     Message=""; 
     Message="Files"; 
     Flags="Trues"; 
     sendpanel.setVisibility(View.GONE); 
     chatpanel.setVisibility(View.VISIBLE); 
     count=0; 
    } 
}; 
    OnClickListener listener1=new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Flags="Trues"; 
      Message="CONTACTS"; 
      sendpanel.setVisibility(View.GONE); 
      chatpanel.setVisibility(View.VISIBLE); 
       count=0; 
     } 
    }; 
b1.setOnClickListener(listener); 
b2.setOnClickListener(listener1); 
Files.setOnClickListener(Listnerfiles); 
back.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     e1.setText(""); 
     Message="CONTACTS"; 

     Flags="Trues"; 
     count=0; 
    } 
}); 
    userList = new ArrayList<ChatClient>(); 
    ChatServerThread chatServerThread = new ChatServerThread(); 
    chatServerThread.start(); 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 

    if (serverSocket != null) { 
    try { 
    serverSocket.close(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 
} 
private class ChatServerThread extends Thread { 

    @Override 
    public void run() { 
    Socket socket = null; 

    try 
    { 
    serverSocket = new ServerSocket(SocketServerPORT); 
    ServerSocketNew.this.runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     infoPort.setText("I'm waiting here: " 
     + serverSocket.getLocalPort()); 
    } 
    }); 

    while (true) { 

    socket = serverSocket.accept(); 
    ChatClient client = new ChatClient(); 
    userList.add(client); 

    ConnectThread connectThread = new ConnectThread(client, socket); 
    connectThread.start(); 

    } 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    if (socket != null) { 
    try { 
     socket.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
    } 

    } 

} 

private class ConnectThread extends Thread { 

    Socket socket; 
    ChatClient connectClient; 
    String msgToSend = ""; 

    ConnectThread(ChatClient client, Socket socket){ 
    connectClient = client; 
    this.socket= socket; 
    client.socket = socket; 
    client.chatThread = this; 
    } 

    @Override 
    public void run() { 
    DataInputStream dataInputStream = null; 
    DataOutputStream dataOutputStream = null; 

    try { 
    dataInputStream = new DataInputStream(socket.getInputStream()); 
    dataOutputStream = new DataOutputStream(socket.getOutputStream()); 

    String n = dataInputStream.readUTF(); 

    connectClient.name = n; 

    msgLog += connectClient.name + " [email protected]" + 
     connectClient.socket.getInetAddress() + 
     ":" + connectClient.socket.getPort() + "\n"; 
    ServerSocketNew.this.runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     infoIp.setText(msgLog); 
    } 
    }); 

    dataOutputStream.writeUTF("Welcome " + n + "\n"); 
    dataOutputStream.flush(); 

    broadcastMsg(n + " join our chat.\n"); 

    while (true) { 
    if (dataInputStream.available() > 0) { 
     String newMsg = dataInputStream.readUTF(); 

     msgLog=""; 
     msgLog = newMsg; 
     ServerSocketNew.this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      // infoIp.setText(msgLog); 
      // Toast.makeText(getApplicationContext(), "prev="+prev, Toast.LENGTH_LONG).show(); 
      // Toast.makeText(getApplicationContext(), "messagelog="+msgLog, Toast.LENGTH_LONG).show(); 
     Log.e("Number",msgLog); 
      e2.setText(msgLog+"#"+msgLog); 
      if(count<5) 
     { 
       Toast.makeText(getApplicationContext(), "messagelog="+msgLog, Toast.LENGTH_LONG).show();  

       contact.setText(msgLog); 

     } 
      count=count+1; 

     } 
     }); 

     broadcastMsg(n + ": " + newMsg); 
    } 

    if(!msgToSend.equals("")){ 
     dataOutputStream.writeUTF(msgToSend); 
     dataOutputStream.flush(); 
     msgToSend = ""; 
    } 

    } 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    if (dataInputStream != null) { 
    try { 
     dataInputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    if (dataOutputStream != null) { 
    try { 
     dataOutputStream.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

    userList.remove(connectClient); 
    ServerSocketNew.this.runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     Toast.makeText(ServerSocketNew.this, 
     connectClient.name + " removed.", Toast.LENGTH_LONG).show(); 

     msgLog += "-- " + connectClient.name + " leaved\n"; 
     ServerSocketNew.this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      infoIp.setText(msgLog); 
     } 
     }); 

     broadcastMsg("-- " + connectClient.name + " leaved\n"); 
    } 
    }); 
    } 

    } 

    private void sendMsg(String msg){ 
    msgToSend = msg; 
    } 

} 

private void broadcastMsg(String msg){ 
    for(int i=0; i<userList.size(); i++){ 
    userList.get(i).chatThread.sendMsg(Message); 
    msgLog = Message+"\n"; 
    } 

    ServerSocketNew.this.runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     infoIp.setText(msgLog); 
    } 
    }); 
} 

我的客戶端代碼

public class CLIENTNEW123 extends ActionBarActivity { 

    static final int SocketServerPORT = 8080; 
    DataOutputStream dataOutputStream = null; 
     DataInputStream dataInputStream = null; 

    LinearLayout loginPanel chatPanel;TextView chatMsg;static String fourth; 


    Button buttonSend; 


    String msgLog = ""; 
    String msgLog1=""; 
    String ret = null; 
    String nameno=""; 
     String ret1 = null; 
     Context context=this; 
     String name=""; 
     String flags="false"; 

    ChatClientThread chatClientThread = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_clientnew123); 
     final Handler handler = new Handler(); 

     loginPanel = (LinearLayout)findViewById(R.id.loginpanel); 
     chatPanel = (LinearLayout)findViewById(R.id.chatpanel); 

     chatMsg = (TextView) findViewById(R.id.chatmsg); 



     msgLog = ""; 
     chatMsg.setText(msgLog); 
     loginPanel.setVisibility(View.GONE); 
     chatPanel.setVisibility(View.VISIBLE); 


     chatClientThread = new ChatClientThread(
     "aaa","192.168.43.1", SocketServerPORT); 
     chatClientThread.start(); 


      Runnable runable = new Runnable() { 

        @Override 
        public void run() { 
         try{ 

          chatClientThread.sendMsg("Namasted"+ "\n"); 
          handler.postDelayed(this, 15*1000); 
          Log.e("In Handler", "In Handler"); 
         } 
         catch (Exception e) { 
          // TODO: handle exception 
         } 
         finally{ 
          //also call the same runnable 
          handler.postDelayed(this, 15*1000); 
         } 
        } 
       }; 
       handler.postDelayed(runable, 15*1000); 
    } 
     private class ChatClientThread extends Thread { 

     String name; 
     String dstAddress; 
     int dstPort; 

     String msgToSend = ""; 
     boolean goOut = false; 

     private File root; 
     private ArrayList<File> fileList = new ArrayList<File>(); 
     private LinearLayout view; 
     String Filenames=""; 

     File[] fileArray; 
     ChatClientThread(String name, String fourth, int port) { 
     this.name = name; 
     dstAddress = fourth; 
     dstPort =8080; 

     } 

     @Override 
     public void run() { 
     Socket socket = null; 


     try { 
     socket = new Socket(dstAddress, dstPort); 
     dataOutputStream = new DataOutputStream(
      socket.getOutputStream()); 
     dataInputStream = new DataInputStream(socket.getInputStream()); 
     dataOutputStream.writeUTF(name); 
     dataOutputStream.flush(); 

     while (!goOut) { 
     if (dataInputStream.available() > 0) { 
      msgLog1 = dataInputStream.readUTF(); 
     // msgLog += msgLog1; 
     // Toast.makeText(getApplicationContext(),msgLog,Toast.LENGTH_LONG).show(); 

      CLIENTNEW123.this.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
      chatMsg.setText(msgLog1); 
      // name=msgLog; 
       //abcd aa=new abcd(); 
      //aa.add(name); 


      } 
      }); 
      //sendMsg("name " + "\n"); 
     } 


     if(!msgToSend.equals("")){ 
      if(msgLog1.equals("FilesNew")) 
      { 
        File file = new File(Environment.getExternalStorageDirectory(),"test.txt"); 
        byte[] bytes = new byte[1024]; 
        InputStream is = socket.getInputStream(); 
        FileOutputStream fos = new FileOutputStream(file); 
        BufferedOutputStream bos = new BufferedOutputStream(fos); 
        int bytesRead = is.read(bytes, 0, bytes.length); 
        bos.write(bytes, 0, bytesRead); 
        bos.close(); 
        socket.close(); 
      } 
      if(msgLog1.equals("Files")) 
      { 
       flags="Files"; 
       root = new File(Environment.getExternalStorageDirectory() 
          .getAbsolutePath()); 
        fileList=getfile(root); 

        for (int i = 0; i < fileList.size(); i++) { 
         Filenames+=fileList.get(i).getName()+"#"+"\n"; 
         System.out.println(fileList.get(i).getName()); 
         if (fileList.get(i).isDirectory()) { 

         } 
              } 
        dataOutputStream.writeUTF(Filenames+"\n"); 
         dataOutputStream.flush(); 
      } 



      else if(msgLog1.equals("CONTACTS")) 
      { 
       flags="true"; 
       dataOutputStream.writeUTF("\n"); 
        dataOutputStream.flush(); 
      } 
      else if(flags=="true") 
      { 
       String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'"+msgLog1+"%'"; 
        String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
          ContactsContract.CommonDataKinds.Phone.NUMBER}; 

        Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
          projection, selection, null, null); 
        int indexName = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
        int indexNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
        // Socket socket = serverSocket.accept(); 
       while(c.moveToNext()) { 
         ret = c.getString(indexName); 
         ret1 = c.getString(indexNumber); 
         nameno=nameno+" "+ret+" "+ret1; 

        } 

       if (c.moveToFirst()) { 
        ret = c.getString(0); 
        ret1 = c.getString(0); 

       } 

        c.close(); 
         dataOutputStream.writeUTF(nameno+"\n"); 
         dataOutputStream.flush(); 
      } 
      else 
      { 
        dataOutputStream.writeUTF("Messgae lo"+msgLog1); 
        dataOutputStream.flush(); 
      } 

      msgToSend = ""; 
     } 
     } 

     } catch (UnknownHostException e) { 
     e.printStackTrace(); 
     final String eString = e.toString(); 
     CLIENTNEW123.this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      Toast.makeText(CLIENTNEW123.this, eString, Toast.LENGTH_LONG).show(); 
     } 

     }); 
     } 
     catch (IOException e) { 
     e.printStackTrace(); 
     final String eString = e.toString(); 
     CLIENTNEW123.this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      Toast.makeText(CLIENTNEW123.this, eString, Toast.LENGTH_LONG).show(); 
     } 

     }); 
     } finally { 
     if (socket != null) { 
     try { 
      socket.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

     if (dataOutputStream != null) { 
     try { 
      dataOutputStream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

     if (dataInputStream != null) { 
     try { 
      dataInputStream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 

     CLIENTNEW123.this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      loginPanel.setVisibility(View.VISIBLE); 
      chatPanel.setVisibility(View.GONE); 
     } 

     }); 
     } 

     } 
     public ArrayList<File> getfile(File dir) { 
      File listFile[] = dir.listFiles(); 
      if (listFile != null && listFile.length > 0) { 
       for (int i = 0; i < listFile.length; i++) { 

        if (listFile[i].isDirectory()) { 
         fileList.add(listFile[i]); 
         getfile(listFile[i]); 

        } else { 
         if (listFile[i].getName().endsWith(".png") 
           || listFile[i].getName().endsWith(".jpg") 
           || listFile[i].getName().endsWith(".jpeg") 
           || listFile[i].getName().endsWith(".gif") 
          || listFile[i].getName().endsWith(".mp3") 
          || listFile[i].getName().endsWith(".pdf")) 

         { 
          fileList.add(listFile[i]); 
         } 
        } 

       } 
      } 
      return fileList; 
     } 

     private void sendMsg(String msg){ 
     msgToSend = msg; 
     } 

     private void disconnect(){ 
     goOut = true; 
     } 
    } 

} 

我舉杯我的字符串中connectthread類 的run方法一樣

Toast.makeText(getApplicationContext(), "messagelog="+msgLog, Toast.LENGTH_LONG).show(); 

之後,我很值分配給TextView的

我得到在祝酒正確的值。

我得到在服務器端的問題,近

if(count<5) 
     { 
       Toast.makeText(getApplicationContext(), "messagelog="+msgLog, Toast.LENGTH_LONG).show();  

       contact.setText(msgLog); 

     } 

這個代碼 在「接觸」的TextView我設置的值提前

+1

你試圖讓'chatMsg.setText(msgLog1)之前另一個日誌或吐司;'?而且,'msgLog' AMD'msgLog1'有什麼區別? – Turtle

+0

chatMsg.setText(msgLog1)在客戶端,但我得到的問題,在服務器端我收到服務器端的值,並吐司它 –

+0

我設置在contact.setText(msgLog)服務器端的值; –

回答

0

既然你在UIThread使用MSGLOG
謝謝, 因此其值取代此線程重複運行方法。 嘗試在MSGLOG追加新的價值不只是更換

msgLog=""; 
while (true) { 
if (dataInputStream.available() > 0) { 
    String newMsg = dataInputStream.readUTF(); 


    msgLog += newMsg; 
    ServerSocketNew.this.runOnUiThread(new Runnable() { 

    @Override 
    public void run() { 
     // infoIp.setText(msgLog); 
     // Toast.makeText(getApplicationContext(), "prev="+prev, Toast.LENGTH_LONG).show(); 
     // Toast.makeText(getApplicationContext(), "messagelog="+msgLog, Toast.LENGTH_LONG).show(); 
    Log.e("Number",msgLog); 
     e2.setText(msgLog+"#"+msgLog); 
     if(count<5) 
    { 
      Toast.makeText(getApplicationContext(), "messagelog="+msgLog, Toast.LENGTH_LONG).show();  
     // Flags="False"; 
     // contact.setText(""); 
     // contact1.setText(""); 
      //contact1.setText(msgLog2); 
      contact.setText(msgLog); 

    } 
     count=count+1; 

    } 
    }); 

    broadcastMsg(n + ": " + newMsg); 
} 
+0

但是當我追加價值我得到的價值與以前的值,我不需要以前的值和部分是值被烤,但沒有分配 –