简体   繁体   English

在服务器上从android应用接收的数据始终为null

[英]data received on server from android app always null

I am trying to connect from a android emulator to a application on my desktop and send a line of text. 我正在尝试从Android模拟器连接到桌面上的应用程序,并发送一行文本。 My app is able to connect to the server, but when ever i try to read data its always null 我的应用程序能够连接到服务器,但是每当我尝试读取数据时,它始终为null

Server application running on my desktop: 在我的桌面上运行的服务器应用程序:

ServerSocket ss = new ServerSocket(9001);
Socket cs = ss.accept();
if (cs.isConnected()) {
    System.out.println("Client connected.");
}

BufferedReader reader = new BufferedReader(new InputStreamReader(cs.getInputStream()));

String str = reader.readLine();
System.out.println("Data:" + str);

Client application running in android app emulator: 在android app模拟器中运行的客户端应用程序:

    InetAddress addr = InetAddress.getByName("10.0.2.2");
    Socket socket = new Socket(addr, 9001);

    if (socket.isConnected()) {
        Log.d("APP", "socket connected");
    }

    PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
    String str = "this is a sample";
    pw.write(str);

I can see that the isConnected function of the socket on both the client and server turns true. 我可以看到客户端和服务器上套接字的isConnected函数都变为true。 But the Data printed on the server is always null. 但是服务器上打印的数据始终为空。

Thanks 谢谢

尝试在“ pw.write(str)”语句之后添加刷新调用:

pw.flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM