2012-06-21 49 views
1

我正在爲Android設備開發TCPclient。 我可以連接到服務器,並且可以跟蹤我從服務器收到的消息。 我想顯示與TextView textview_textin在TextView中的Android TCP客戶端讀取套接字

上的客戶端GUI服務器發送的處理結果,你能不能幫我做

非常感謝

public class JssclientActivity extends Activity { 
private EditText serverIp; 

private Button connectPhones; 

private String serverIpAddress = "192.168.0.2"; 

private boolean connected = false; 

private TextView textview_textin; 



public class ClientThread implements Runnable { 

    public void run() { 
     try { 

      InetAddress serverAddr = InetAddress.getByName(serverIpAddress); 
      Log.d("ClientActivity", "C: Connecting..."); 
      Socket socket = new Socket(serverAddr, 2600); 
      connected = true; 
      while (connected) { 
       try { 

        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
        String inMsg = ""; 
        char buf[] = null; 
        int val = in.read(); 
        while (-1 != val) { 
         inMsg = in.readLine(); 
        } 

       } catch (Exception e) { 
        Log.e("ClientActivity", "S: Error", e); 
       } 
      } 
      socket.close(); 
      Log.d("ClientActivity", "C: Closed."); 
     } catch (Exception e) { 
      Log.e("ClientActivity", "C: Error", e); 
      connected = false; 
     } 
    } 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    textview_textin = (TextView) findViewById(R.id.textin); 
    connectPhones = (Button) findViewById(R.id.connect); 

    connectPhones.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (!connected) { 
      //serverIpAddress = serverIp.getText().toString(); 
      if (!serverIpAddress.equals("")) { 
       Thread cThread = new Thread(new ClientThread()); 
       cThread.start(); 
       }    
     } 
    } 
}); 

} 
} 

非常感謝爲您的回覆

我可以連接到設備,但我什麼都沒收到。我不知道爲什麼?

這裏是我的嘗試,

public class JssclientActivity extends Activity { 
private EditText serverIp; 

private Button connectPhones; 

private String serverIpAddress = "192.168.20.21"; 

private boolean connected = false; 

private TextView textview_textin; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    textview_textin = (TextView) findViewById(R.id.textin); 
connectPhones = (Button) findViewById(R.id.connect); 



ClientThread ct = new ClientThread(new ClientThread.SocketCallback() { 
     public void onReceived(final String msg) { 
     JssclientActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       textview_textin.setText(msg); 
      } 
     }); 
     } 
    }); 

    } 
} 




class ClientThread implements Runnable { 

interface SocketCallback { 
    void onReceived(String msg); 
} 
private SocketCallback callback; 

private boolean connected; 

private Socket socket; 

public ClientThread(ClientThread.SocketCallback cb) { 
     this.callback = cb; 
     try {InetAddress serverAddr = InetAddress.getByName("192.168.20.21"); 
     Log.d("ClientActivity", "C: Connecting..."); 
     //Socket 
    socket = new Socket(serverAddr, 2600); 
     connected = true; 
     } catch (Exception e) { 
      Log.e("ClientActivity", "C: Error", e); 
      connected = false; 
     } 
    } 

public void run() { 
     while (connected) { 
      try { 

       BufferedReader in = new BufferedReader(new  InputStreamReader(socket.getInputStream())); 
       String inMsg = ""; 
       char buf[] = null; 
       int val = in.read(); 
      //  while (-1 != val) { 
      //  inMsg = in.readLine(); 
      // } 
       while (-1 != val) { 
        inMsg = in.readLine(); 
        this.callback.onReceived(inMsg); 
       } 

      } catch (Exception e) { 
       Log.e("ClientActivity", "S: Error", e); 
      } 
     } 
    // socket.close(); 
     Log.d("ClientActivity", "C: Closed."); 

} 
} 

請你能幫助我嗎?

感謝

+0

您應該編輯您的問題並重新放置查詢,而不是將其作爲答案。所以我建議刪除這個答案,並編輯你的主要問題,因爲用戶來到這個帖子將檢查問題不是這樣,所以它會更好地獲得rellavent答案。 – MKJParekh

回答

0

您可以使用它的方法setText()設置一個TextView的文本。

例子:

textview_textin.setText(yourText) 

使用runOnUiThread()您將能夠訪問你的TextView。見this answer

1

創建一個回調類,

interface SocketCallback { 
    void onReceived(String msg); 
} 

這可能是一個內部的ClientThread,因爲他們有邏輯關係。這不是必需的,但它是很好的設計。

有你的Runnable類在它的構造函數中接受這個回調的一個實例。

public ClientThread(ClientThread.SocketCallback cb) { 
    this.callback = cb; 
} 

當你構建你的RunnableClientThread),從你的UI類(JssClientActivity)這樣做,這樣,

ClientThread ct = new ClientThread(new ClientThread.SocketCallback() { 
    public void onReceived(String msg) { 
    JssClientActivity.this.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
     myTextView.setText(msg); 
     } 
    }); 
    } 
}}; 

最後,在ClientThread,當您收到一條消息,調用回調,

while (-1 != val) { 
    inMsg = in.readLine(); 
    this.callback.onReceived(inMsg); 
} 

請注意,您無法從運行ClientThread的線程更新UI。它必須從UI線程更新,因此致電runOnUiThread()

+0

感謝您的回覆, – Vestaproman

+0

如果這回答了您的問題,請點擊答案左側複選標記的大綱接受它。 –