-1

I'm calling a BufferedReader to get a HTTP response body (if it exists) and stick it in one long string variable. Sometimes when I attempt to do this I get the error java.io.IOException: stream is closed sometimes when the while loop below starts to execute. I don't understand why. I'd like to make sure the object isn't null and that is has a response body before reading in the object.

        BufferedReader readBuffer = null;
        if (connection.getResponseCode() >= 200 && connection.getResponseCode() <= 299) {
            readBuffer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } else {
            readBuffer = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        }

        if(readBuffer != null) {
          // Get the response body output from the server
          StringBuilder calculatedOutput = new StringBuilder();
          String rawOutputLine;
          while ((rawOutputLine = readBuffer.readLine()) != null) {
              calculatedOutput.append(rawOutputLine);
          }

          Logger.debug(String.format("BODY: %s", calculatedOutput.toString()));
          readBuffer.close();
2
  • Maybe the other side of the connection has closed the Stream. Commented Mar 9, 2018 at 19:10
  • it might be helpful if we could see more of the code Commented Mar 9, 2018 at 19:19

2 Answers 2

1

Try this to read the response:

    BufferedReader readBuffer = null;

    try {
        if (connection.getResponseCode() >= 200 && connection.getResponseCode() <= 299) {
            readBuffer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } else {
            readBuffer = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
        }
        // Get the response body output from the server
        StringBuilder calculatedOutput = new StringBuilder();
        String rawOutputLine;
        while ((rawOutputLine = readBuffer.readLine()) != null) {
            calculatedOutput.append(rawOutputLine);
        }
        Logger.debug(String.format("BODY: %s", calculatedOutput.toString()));
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            readBuffer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Also, I think you could check BufferedReader's ready() function it would be helpful in your case.
@dan did this one solved your issue or add a hint to the solution?
1

Try this code:

   BufferedReader readBuffer = null;
    if (connection.getResponseCode() >= 200 && connection.getResponseCode() <= 299) {
        readBuffer = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    } else {
        readBuffer = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
    }

    if(readBuffer != null) {
      // Get the response body output from the server
      StringBuilder calculatedOutput = new StringBuilder();
      String rawOutputLine;
      if(readBuffer.ready()) {
          while ((rawOutputLine = readBuffer.readLine()) != null) {
              calculatedOutput.append(rawOutputLine);
          }
      }
      Logger.debug(String.format("BODY: %s", calculatedOutput.toString()));
      readBuffer.close();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.