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,

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • Aren't there size restrictions on HTTP GET requests? https://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request – duffymo Nov 18 '15 at 11:30
  • I am using POST not GET i.e. mHttpUrlConnection.setRequestMethod("POST"); – ant2009 Nov 18 '15 at 11:33

3 Answers3

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;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Arpit Agrawal
  • 321
  • 1
  • 2
  • 14
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.

sibnick
  • 3,995
  • 20
  • 20
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.

Mike Murphy
  • 1,006
  • 8
  • 16