我正在构建一个非常简单的应用程序,它由一个客户端组成,用户在其中输入一个发送到服务器的字符串。然后,一旦服务器收到消息,它就会将相同的消息发送回客户端。我的问题是,当我尝试读取服务器发送的消息时,客户端出现错误。
打开套接字后,以下代码段显示了客户端如何尝试发送和接收消息:
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())),true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(message);
if (in.readLine()!=null)
response=in.readLine();
读取“in”变量 (in.readLine()) 时出现 strictMode$AndroidBlockGuardPolicy.onNetwork() 异常
服务器端如下:
while ((line = in.readLine()) != null) {
final String line2=line;
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText(line2);
}
});
out.println("Message received:"+line2);
我唯一想要的是向客户端发送一条消息,确认已收到服务器上的一条消息。我面临的另一个问题是需要让服务器有时间接收消息,读取它并将其发送回客户端,因此客户端应该等待一秒钟。我可以为此目的使用什么?睡眠方法合适吗?
Thread.sleep(1000);
或者
systemClock.sleep(1000);
提前致谢
[更新]
这是客户端的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
serverIp = (EditText) findViewById(R.id.server_ip);
connectPhones = (Button) findViewById(R.id.connect_phones);
connectPhones.setOnClickListener(connectListener);
messageServer = (EditText) findViewById(R.id.message);
communicatePhones = (Button) findViewById(R.id.send_message);
messageServer.setVisibility(View.GONE);
communicatePhones.setVisibility(View.GONE);
}
private OnClickListener connectListener = new 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 ClientThread implements Runnable {
public void run() {
try {
Log.d("ClientActivity", serverIpAddress);
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
handler.post(new Runnable() {
@Override
public void run() {
serverIp.setVisibility(View.GONE);
connectPhones.setVisibility(View.GONE);
messageServer.setVisibility(View.VISIBLE);
communicatePhones.setVisibility(View.VISIBLE);
communicatePhones.setOnClickListener(communicateListener);
mensajeRecibido = (TextView) findViewById(R.id.mensaje_recibido);
mensajeRecibido.append(response);
}
});
//socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
private OnClickListener communicateListener = new OnClickListener() {
@Override
public void onClick(View v) {
message = messageServer.getText().toString();
if (!serverIpAddress.equals("")) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(message);
Log.d("ClientActivity", "C: Sent.");
//Thread.sleep(6000);
SystemClock.sleep(2000);
/* handler.postDelayed(new Runnable() {
public void run() {
my_button.setBackgroundResource(R.drawable.defaultcard);
}
}, 2000); */
if (in.readLine()!=null)
response=in.readLine();
} catch (IOException e) {
Log.e("ClientActivity", "S: Error", e);
}
}
}
};
}
}
我应该在哪里为communicateListener 创建OnClickListener 对象?我试图将它放在 run 方法中,但出现了未知的communicateListener 变量或无效的私有修饰符之类的错误。