Android客户端服务器应用程序 - readLine()不起作用

问题描述:

我正在编写一个基于Socket的客户端(android)服务器(java)应用程序。我的问题是我需要在服务器上处理两种类型的消息(MINDWAVE和SPHERO)。 mindwave消息由服务器处理得很好,但是我遇到了sphero问题: -client发送消息“SPHERO”来断开连接。 - 服务器打印“Sphero请求抓取”。并通过其余代码罚款 - 客户端在其“while((fromServer = in.readLine())!= null)”循环(它甚至不启动循环的第一个操作 - 只是在readline )部分)。Android客户端服务器应用程序 - 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(); 
     } 

    } 
} 
+1

为什么睡觉?它们实际上是浪费时间。 – EJP 2014-09-03 20:03:45

readLine()只有当它读取一个新行或在流关闭返回。所以你应该发送"SPHERO\n"