简体   繁体   English

为什么我的套接字没有收到响应?

[英]Why doesn't my socket receive the response?

I opened a regular java socket in my android app, and I have to use it to send requests and receive responses, I am sending the requests normally but don't receive any response from the server, I checked the server connections and it's alright and also I tried to listen via Hercules and the requests are sent normally and the server sends the responses normally, this is the regular socket coding I'm using: 我在我的Android应用程序中打开了一个常规的Java套接字,我必须用它来发送请求和接收响应,我正在发送请求,但是没有从服务器收到任何响应,我检查了服务器连接,它没关系,我也尝试通过Hercules听取请求正常发送,服务器正常发送响应,这是我正在使用的常规套接字编码:

public static void xbmc_connect(){
    try {
        xbmc_socket = new Socket(xbmc_address, xbmc_port);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void xbmc_sendingDataToSystem(String data) throws IOException{

    xbmc_output = new DataOutputStream(xbmc_socket.getOutputStream());
    xbmc_output.writeBytes(data);

}

public String xbmc_getServerResponse() throws JSONException{

    String responseLine, server_response = null_string;

      try {
          xbmc_input = new BufferedReader(new InputStreamReader(
                    xbmc_socket.getInputStream()));
          System.out.println(xbmc_input.toString());// doesn't print anything
    } catch (IOException e) {
    }
  try {
        while ((responseLine = xbmc_input.readLine()) != null) {
            server_response = server_response + responseLine ;
      }

            return server_response;

}

In short: xbmc_input is a BufferedReader . 简而言之: xbmc_input是一个BufferedReader To read from a BufferedReader, you have to use either read() or readLine() . 要从BufferedReader读取,您必须使用read()readLine()

It doesn't make much sense to get the String representation of the BufferedReader with toString() as you are doing. 使用toString()获取BufferedReader的String表示没有多大意义。 Calling toString() does not make the BufferedReader print anything that it may have received. 调用toString()不会使BufferedReader打印出它可能收到的任何内容。 toString() simply prints the BufferedReader object - that is, a reference to the object . toString()只是打印BufferedReader对象 - 对象 的引用

Therefore, to print what was actually received (if anything), you may want to try: 因此,要打印实际收到的内容(如果有的话),您可能需要尝试:

System.out.println(xbmc_input.readLine());

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

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