Java Android客户端服务器应用程序-readLine()不支持';行不通
我正在编写一个基于套接字的客户端(android)-服务器(java)应用程序。我的问题是,我需要在服务器上处理两种类型的消息(MINDWAVE和SPHERO)。服务器对mindwave消息进行了很好的处理,但sphero one有一个问题: -客户端向服务器发送消息“SPHERO”。 -服务器打印“Sphero请求已捕获”,并仔细检查其余代码 -客户端阻塞其“while((fromServer=in.readLine())!=null)”循环(它甚至没有启动循环的第一个操作-只是阻塞readLine()部分) 客户端线程Java Android客户端服务器应用程序-readLine()不支持';行不通,java,android,client-server,Java,Android,Client Server,我正在编写一个基于套接字的客户端(android)-服务器(java)应用程序。我的问题是,我需要在服务器上处理两种类型的消息(MINDWAVE和SPHERO)。服务器对mindwave消息进行了很好的处理,但sphero one有一个问题: -客户端向服务器发送消息“SPHERO”。 -服务器打印“Sphero请求已捕获”,并仔细检查其余代码 -客户端阻塞其“while((fromServer=in.readLine())!=null)”循环(它甚至没有启动循环的第一个操作-只是阻塞readL
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;
}
}
类SendSphereRequest扩展了异步任务{
字符串fromServer=“”;
智力运动;
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
《睡眠》(2000年);
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
while(isActive){
试一试{
套接字=新套接字(地址、端口);
睡眠(1000);
out=新的PrintWriter(socket.getOutputStream(),true);
写出(标签);
out.flush();
out.close();
套接字=新套接字(地址、端口);
in=新的BufferedReader(新的InputStreamReader(
getInputStream());
而((fromServer=in.readLine())!=null){
Toast.makeText(getApplicationContext(),fromServer,
吐司。长度(短)。show();
if(!fromServer.equalsIgnoreCase(“”){
试一试{
movement=Integer.parseInt(fromServer);
如果(移动>0){
driveUp();
}否则如果(移动<0){
driveDown();
}
tvPosition.setText(移动+“”);
}捕获(例外e){
e、 printStackTrace();
移动=0;
}
fromServer=“”;
}
}
睡眠(1000);
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
试一试{
socket.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回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();
}
}
}
public void processMessage(字符串消息){
消息对象=新消息(消息);
if(messageObject.getClientType()==设备类型.MINDWAVE){
System.out.println(“消息发送人”
+messageObject.getClientType()+“ID为的”
+messageObject.getClientID()+“。注意值为”
+messageObject.getAttention());
开关(messageObject.getClientID()){
案例1:{
if(firstClientIterator<5&&gameStarted
&&messageObject.getAttention()!=0){
firstClientAttentionSum+=messageObject.getAttention();
firstClientIterator++;
System.out.println(“sum=“+firstclientattentium
+“迭代器=“+firstClientIterator”);
}
}
打破
案例2:{
if(secondClientIterator<5&&gameStarted
&&messageObject.getAttention()!=0){
secondClientAttentionSum+=messageObject.getAttention();
第二个clientiterator++;
System.out.println(“sum=“+secondclientattention
+“迭代器=“+secondClientIterator”);
}
}
打破
违约:
System.err
.println(“无法处理消息。提示:检测到错误的id”);
}
}else if(messageObject.getClientType()==DEVICE\u TYPE.SPHERO){
System.out.println(“Sphereo请求捕获”);
试一试{
toClientPrintWriter=新的PrintWriter(clientSocket.getOutputStream(),true);
if(firstClientIterator==5&&secondClientIterator==5){
int差=第一客户注意事项
-第二次客户关注;
System.out.println(“将数据发送到Sphereo。”
+“差额为“+差额+”);
firstClientIterator=secondClientIterator=firstClientAttentionSum=secondClientAttentionSum=0;
toClientPrintWriter.println(差异+“”);
}否则{
println(“还没有结果”);
}
toClientPrintWriter.flush();
toClientPrintWriter.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
readLine()
仅在读取换行符或流关闭时返回。因此,您应该发送“SPHERO\n”
为什么睡眠?他们简直是在浪费时间。