Android TCP客户端在TextView中读取套接字

时间:2012-06-21 22:30:03

标签: android tcpclient

我正在为Android设备开发TCPclient。 我可以连接到服务器,我可以跟踪我从服务器收到消息。 我想在客户端GUI上使用TextView textview_textin

显示服务器发送的结果

你能帮我做那个

吗?

非常感谢

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

 }
 }

请你能帮帮我吗?

由于

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

您可以使用setText()方法设置TextView的文本。

示例:

textview_textin.setText(yourText)

使用runOnUiThread(),您将能够访问TextView。请参阅this answer