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

时间:2014-09-03 17:47:26

标签: java android client-server

我正在编写一个基于Socket的客户端(android)-server(java)应用程序。我的问题是我需要在服务器上处理两种类型的消息(MINDWAVE和SPHERO)。 mindwave消息由服务器很好地处理,但是我遇到了sphero问题: -client发送消息" SPHERO"切断 -server print" Sphero请求被捕获。"并完成其余的代码 -client卡在" 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();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

readLine()仅在读取换行符或流关闭时返回。所以你应该发送"SPHERO\n"