[英]Android client-server app - readLine() does't work
我正在寫一個基於Socket的客戶端(android)-服務器(java)應用。 我的問題是我需要處理服務器上的兩種類型的消息(MINDWAVE和SPHERO)。 服務器可以很好地處理mindwave消息,但是sphero一個存在問題:-client將消息“ SPHERO”發送給服務器。 -server打印“捕獲了Sphero請求”。 並仔細檢查其余代碼-客戶端卡在“ while((fromServer = in.readLine())!= null)”循環中(它甚至沒有啟動循環的第一個操作-只是卡在readline()上部分)。
客戶端線程
class SendSpheroRequest extends AsyncTask<Void, Void, Void> {
String fromServer = "";
int movement;
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (isActive) {
try {
socket = new Socket(address, port);
Thread.sleep(1000);
out = new PrintWriter(socket.getOutputStream(), true);
out.write(TAG);
out.flush();
out.close();
socket = new Socket(address, port);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
while ((fromServer = in.readLine()) != null) {
Toast.makeText(getApplicationContext(), fromServer,
Toast.LENGTH_SHORT).show();
if (!fromServer.equalsIgnoreCase("")) {
try {
movement = Integer.parseInt(fromServer);
if (movement > 0) {
driveUp();
} else if (movement < 0) {
driveDown();
}
tvPosition.setText(movement + "");
} catch (Exception e) {
e.printStackTrace();
movement = 0;
}
fromServer = "";
}
}
Thread.sleep(1000);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
和服務器的消息處理:
public void processMessage(String message) {
Message messageObject = new Message(message);
if (messageObject.getClientType() == DEVICE_TYPE.MINDWAVE) {
System.out.println("Message sent by "
+ messageObject.getClientType() + " with ID="
+ messageObject.getClientID() + ". The attention value is "
+ messageObject.getAttention());
switch (messageObject.getClientID()) {
case 1: {
if (firstClientIterator < 5 && gameStarted
&& messageObject.getAttention() != 0) {
firstClientAttentionSum += messageObject.getAttention();
firstClientIterator++;
System.out.println("sum=" + firstClientAttentionSum
+ " iterator=" + firstClientIterator);
}
}
break;
case 2: {
if (secondClientIterator < 5 && gameStarted
&& messageObject.getAttention() != 0) {
secondClientAttentionSum += messageObject.getAttention();
secondClientIterator++;
System.out.println("sum=" + secondClientAttentionSum
+ " iterator=" + secondClientIterator);
}
}
break;
default:
System.err
.println("Cannot process the message. Hint: wrong id detected.");
}
} else if (messageObject.getClientType() == DEVICE_TYPE.SPHERO) {
System.out.println("Sphero request catched.");
try {
toClientPrintWriter = new PrintWriter(clientSocket.getOutputStream(), true);
if (firstClientIterator == 5 && secondClientIterator == 5) {
int difference = firstClientAttentionSum
- secondClientAttentionSum;
System.out.println("Sending data to Sphero. "
+ "The difference is " + difference + ".");
firstClientIterator = secondClientIterator = firstClientAttentionSum = secondClientAttentionSum = 0;
toClientPrintWriter.println(difference+"");
} else {
toClientPrintWriter.println("No results yet.");
}
toClientPrintWriter.flush();
toClientPrintWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
readLine()
僅在讀取換行符或流關閉時返回。 因此,您應該發送"SPHERO\\n"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.