1

我正在为 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.");

 }
 }

请问你能帮帮我吗?

谢谢

4

2 回答 2

1

创建一个回调类,

interface SocketCallback {
  void onReceived(String msg);
}

这可以是 的内部ClientThread,因为它们在逻辑上是相关的。这不是必需的,但它是很好的设计。

让您的Runnable类在其构造函数中接受此回调的实例。

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

当你构建你的Runnable( ClientThread) 时,从你的 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().

于 2012-06-21T22:38:18.020 回答
0

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

例子:

textview_textin.setText(yourText)

通过使用runOnUiThread(),您将能够访问您的 TextView。看到这个答案

于 2012-06-21T22:35:34.387 回答