0
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)

Hello,

I am using the httpUrlConnection to retrieve a json string from a webservice. Then I get the inputStream from the connection

jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());

I then use the following function to read the inputstream to get the JSON.

private String readJSONInputStream(final InputStream inputStream) {
    log.log(Level.INFO, "readJSONInputStream()");

    Reader reader = null;

    try {
        final int SIZE = 16092;

        char[] buffer = new char[SIZE];
        int bytesRead = 0;
        int read = 0;

        reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);
        bytesRead = reader.read(buffer);

        String jsonString = new String(buffer, 0, bytesRead);
        log.log(Level.INFO, "bytesRead: " + bytesRead + " [ " + jsonString + " ]");
        /* Success */
        return jsonString;
    }
    catch(IndexOutOfBoundsException ex) {
        log.log(Level.SEVERE, "UnsupportedEncodingexception: " + ex.getMessage());
    }
    catch(IOException ex) {
        log.log(Level.SEVERE, "IOException: " + ex.getMessage());
    }
    finally {
        /* close resources */
        try {
            reader.close();
            inputStream.close();
        }
        catch(IOException ex) {
            log.log(Level.SEVERE, "IOException: " + ex.getMessage());
        }
    }

    return null;
}

However, if the json is small say 600 bytes then everything is ok (so the code above does work for smaller data). But I have some JSON that is about 15000 bytes in size so I set the maximum size to 16092.

However, the JSON it only reads about about 6511 and just cuts off.

I don't understand that if the JSON is small there is no problem. But for the larger JSON it just cuts off at the same size each time.

Am I doing anything wrong here. Anything I should check.

Many thanks for any suggestions,

2
  • Aren't there size restrictions on HTTP GET requests? stackoverflow.com/questions/2659952/… Commented Nov 18, 2015 at 11:30
  • I am using POST not GET i.e. mHttpUrlConnection.setRequestMethod("POST"); Commented Nov 18, 2015 at 11:33

3 Answers 3

3

Try the following code:

String readResponse(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while ((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Testing your code now. Should you not test for -1 instead of null in the while loop?
It is working totally fine, I have used the same code many times. Is it causing you a trouble??
The return is still truncated/cut for me
2

Most likely your code is broken here:

bytesRead = reader.read(buffer);

You should have cycle around read. I think you do not read all chars from stream.

Comments

1

You should continue to read from the buffer until bytesRead = -1.

I suspect that the reader is buffering the data and only returning a certain amount of data at a time.

Try looping until bytesRead == -1 with each loop append the read data to your resultant String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.