[英]Send data from Android mobile to java server
我尝试将数据从Android手机发送到Java服务器。 我完成了服务器应用程序,并通过启动“ ClientConnexion”线程直接从中模拟了一个客户端。 它工作正常,数据即将来临,我可以读取它。
然后,将“ ClientConnexion”类复制到我的Android手机上。
在服务器上调试时,我看到正确建立了连接,但是读取功能正在阻塞,就好像从未传入数据一样。 但是,在Android端调试表明它(似乎)已发送。
这是我的ClientConnexion代码:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;
public class Client implements Runnable{
private Socket connexion = null;
private PrintWriter writer = null;
private BufferedInputStream reader = null;
public Client(String host, int port){
try {
connexion = new Socket(host, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
try {
writer = new PrintWriter(connexion.getOutputStream(), true);
reader = new BufferedInputStream(connexion.getInputStream());
// COMMAND is received by server when emulating client directly from it
// COMMAND is NOT received when sent from Android mobile
writer.write("COMMAND");
writer.flush();
System.out.println("COMMAND sent");
String response = read();
System.out.println("Response : " + response);
}
catch (IOException e1) {
e1.printStackTrace();
}
writer.write("CLOSE");
writer.flush();
writer.close();
}
private String read() throws IOException{
String response = "";
int stream;
byte[] b = new byte[4096];
stream = reader.read(b);
response = new String(b, 0, stream);
return response;
}
}
Android客户端可能会阻止什么? 没有异常被抛出。
确保您的连接正常,也许您的android客户端和服务器在不同的网段中? 最后,您可以调试或登录客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.