MRefaat
MRefaat

Reputation: 535

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:

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;

}

Upvotes: 0

Views: 89

Answers (1)

Martin Dinov
Martin Dinov

Reputation: 8825

In short: xbmc_input is a BufferedReader. To read from a BufferedReader, you have to use either read() or readLine().

It doesn't make much sense to get the String representation of the BufferedReader with toString() as you are doing. Calling toString() does not make the BufferedReader print anything that it may have received. toString() simply prints the BufferedReader object - that is, a reference to the object.

Therefore, to print what was actually received (if anything), you may want to try:

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

Upvotes: 2

Related Questions