cuốn sách gpt4 ai đã làm

Android 简单 2 路 IM 问题(可运行)

In lại Tác giả: Walker 123 更新时间:2023-11-29 02:12:05 27 4
mua khóa gpt4 Nike

我正在尝试测试一个非常简单的双向 IM 应用程序。客户端在 android 上,服务器在我的 PC(java)上。我已经在 PC 到 PC 之间用 java 测试了这个应用程序,它工作正常。

但是在我的 android 模拟器上,当我从我的 PC 向我的 android 模拟器发送一条消息时,只有当它从我的 PC 发送两次时,该消息才会显示在我的 android 上。在 PC 上单击一次发送按钮,没有任何显示,再次单击该消息然后在 android 上显示该消息。

我认为我的可运行方法有问题,但我不完全确定,我希望有人能指出这个问题。提前致谢。

static Socket kkSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;

static String msg;
TextView tv;
EditText et;

Handler mHandler = new Handler();

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

tv = (TextView) findViewById(R.id.tv_message);
et = (EditText) findViewById(R.id.et_message);

Button btnSend = (Button) findViewById(R.id.btn_send);
btnSend .setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {

out.println(et.getText());

}
});

Thread t = new Thread() {
public void run() {
processThread();
}
};

t.start();

}

private void processThread() {

thử {
kkSocket = new Socket("xxx.xx.xx.xxx", 4444);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thử {
out = new PrintWriter(kkSocket.getOutputStream(), true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


thử {
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


thử {
while (in.readLine() != null)
{
msg = in.readLine();
mHandler.post(mUpdateResults);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

Runnable mUpdateResults = new Runnable() {
public void run() {
tv.setText("Server: " + msg);
}
};

câu trả lời hay nhất

是否在每条消息后发送一个换行符?

缓冲的读取器可能正在阻塞等待换行符。

关于Android 简单 2 路 IM 问题(可运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6722645/

27 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress