我的客户端/服务器应用有问题。 客户端在Android上,服务器是java应用程序。
当我在java桌面应用程序上启动服务器和客户端时,一切都很好。但是,当我启动Android应用程序时,有bug。来自服务器的消息在android上可见,但消息(来自android本身或其他客户端)不是。
Android app look || What communicates should be here
没有错误,但消息不会显示。
@Update 在android上我也看不到来自客户端本身的消息,也来自其他聊天客户端的消息。我只看到有人加入聊天。我认为沟通有问题吗?但我不知道是什么。
@ UPDATE2 我注意到程序没有执行这部分代码
protected Void doInBackground(String... params)
{
message = params[0].toString();
Log.e("CLIENT","Message In Background: "+message);
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
Log.e("CLIENT","Message send"+message);
}
return null;
}
我调用它但在日志监视器中我没有收到来自Sender类的消息
Log.e("CLIENT","Create sender");
Sender sender = new Sender();
sender.execute(message);
Log.e("CLIENT","sender.execute");
消息:
01-17 17:15:39.330 8232-8232/com.example.testmulti E/Home: Application start
01-17 17:15:39.596 8232-8259/com.example.testmulti E/EGL_emulation: tid 8259: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-17 17:15:52.137 8232-8232/com.example.testmulti E/ServerIP: Server ip is: 192.168.0.102
01-17 17:15:52.266 8232-8232/com.example.testmulti E/MainActivity: Start Main Activity
01-17 17:15:52.325 8232-8262/com.example.testmulti E/serverAddr: /192.168.0.102
01-17 17:15:52.325 8232-8262/com.example.testmulti E/TCP Client: C: Connecting...
01-17 17:15:52.342 8232-8262/com.example.testmulti E/TCP Server IP: 192.168.0.102
01-17 17:15:52.343 8232-8262/com.example.testmulti E/TCP Client: C: Sent.
01-17 17:15:52.343 8232-8262/com.example.testmulti E/TCP Client: C: Done.
01-17 17:15:52.560 8232-8259/com.example.testmulti E/EGL_emulation: tid 8259: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-17 17:15:59.416 8232-8232/com.example.testmulti E/MainActivity: Button was clicked
01-17 17:15:59.417 8232-8232/com.example.testmulti E/CLIENT: Create sender
01-17 17:15:59.419 8232-8232/com.example.testmulti E/CLIENT: sender.execute
因此它执行 sender.execute(消息);
如何重复?
MainActivity
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<String>();
final EditText editText = (EditText) findViewById(R.id.editText);
Button send = (Button)findViewById(R.id.send_button);
mList = (ListView)findViewById(R.id.list);
mAdapter = new MyCustomAdapter(this, arrayList);
mList.setAdapter(mAdapter);
new connectTask().execute("");
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = editText.getText().toString();
if (mClient != null) {
mClient.sendMessage(message);
}
mAdapter.notifyDataSetChanged();
editText.setText("");
}
});
}
public class connectTask extends AsyncTask<String,String,Client> {
@Override
protected Client doInBackground(String... message) {
mClient = new
Client(new Client.OnMessageReceived() {
@Overrid
public void messageReceived(String message) {
publishProgress(message);
}
});
mClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
arrayList.add(values[0]);
mAdapter.notifyDataSetChanged();
}
}
客户端类
public void sendMessage(String message)
{
Sender sender = new Sender();
sender.execute(message);
}
public void run() {
mRun = true;
try {
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.e("serverAddr", serverAddr.toString());
Log.e("TCP Client", "C: Connecting...");
Socket socket = new Socket(serverAddr, SERVERPORT);
Log.e("TCP Server IP", SERVERIP);
try {
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
Log.e("TCP Client", "C: Sent.");
Log.e("TCP Client", "C: Done.");
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while (mRun) {
serverMessage = in.readLine();
if (serverMessage != null && mMessageListener != null) {
mMessageListener.messageReceived(serverMessage);}
serverMessage = null;
}
Log.e("RESPONSE FROM SERVER", "S: Received Message: '"
+ serverMessage + "'");
} catch (Exception e) {Log.e("TCP", "S: Error", e);
}finally {socket.close();}
} catch (Exception e) {Log.e("TCP", "C: Error", e);}
}
public class Sender extends AsyncTask<String, String, Void>
{
private String message;
@Override
protected Void doInBackground(String... params)
{
message = params[0].toString();
if (out != null && !out.checkError()) {
out.println(message);
out.flush();
}
return null;
}
}
答案 0 :(得分:0)
也许您必须在发送的onClick中将消息添加到列表中?
[...]
arrayList.add(message);
mAdapter.notifyDataSetChanged();
[...]