我在客户端有一个方法,当服务器发送<PING>
消息时,它会将字符串<PING>
发送到服务器。该方法称为parseMessage(String message)
。出于某种原因,它只识别在服务器第一次发送此消息时发送了<PING>
消息。我知道服务器正在正确发送邮件,因为publishProgress()
正确地将其附加到arrayList
并显示在屏幕上。
任何人都可以解释造成这种行为的原因吗?以下是相关代码:
public class connectTask extends AsyncTask<String,String,TCPClient> {
@Override
protected TCPClient doInBackground(String... message) {
//we create a TCPClient object and
mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
@Override
//here the messageReceived method is implemented
public void messageReceived(String message) {
//this method calls the onProgressUpdate
publishProgress(message);
parseMessage(message);
}
});
mTcpClient.run();
return null;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
//in the arrayList we add the messaged received from server
arrayList.add(values[0]);
// notify the adapter that the data set has changed. This means that new message received
// from server was added to the list
mAdapter.notifyDataSetChanged();
}
private void parseMessage(String theMessage)
{
if (theMessage.equals("<PING>"))
{
if (mTcpClient != null)
{
mTcpClient.sendMessage("<PING>");
}
}
else
{
mTcpClient.sendMessage(theMessage);
}
}
}