0
Android Studio 1.5

I have built a java library that I copy into Android Studio. The java library works ok running in my test cases running on Linux. However, when I copy the library into Android studio and test it on actual devices. It fails to read all the JSON. Only ~half gets read. The JSON is only about 15238 bytes. However, I have other JSON which are smaller about 1000 bytes that work ok with this code.

Is there some kind of limit on Android? I am using the httpUrlConnection and POST

jsonString = readJSONInputStream(mHttpUrlconnection.getInputStream());

And the function:

private String readJSONInputStream(InputStream inputStream) {
   try {
        final int SIZE = 8024;
        reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), SIZE);           
        String line = "";
        String jsonString = "";

        while((line = reader.readLine()) != null) {
            jsonString += line;
        }
        log.log(Level.INFO, "JSONSTRING: " + jsonString);

        /* Success */
        return jsonString;
    }
    catch(...) {
    .
    }
}

I have also tried using this. Which works in my test cases on Linux. But fails to read all the data on Android.

 private String readJSONInputStream(InputStream inputStream) {
       try {
             String jsonString = IOUtils.toString(inputStream, "UTF-8");
            /* Success */
            return jsonString;
        }
        catch(...) {
        .
        }
    }

Code that uses the HttpUrlConnection

  try {
            URL url = null;
            HttpsURLConnection connection = null;
            OutputStreamWriter outputStream = null;
            int responseCode = -1;

            url = new URL(cnn_url);

            connection = (HttpsURLConnection)url.openConnection();    
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json; charset=utf8");
            connection.setRequestProperty("Accept", "application/json");
            connection.setUseCaches(true);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setConnectTimeout(15000); /* msecs i.e. 15 seconds */

            /* Send POST request with JSON object */
            outputStream = new OutputStreamWriter(connection.getOutputStream());
            outputStream.write(jsonObject.toString(), 0, jsonObject.toString().length());
            outputStream.flush();
            log.log(Level.INFO, "Connected to server OK");

            /* Get response */
            responseCode = connection.getResponseCode();
            log.log(Level.INFO, "Returned responseCode [ " + responseCode + " ]");

            String jsonString = null;

            if(responseCode == 200) {
                /* Read contents of inputstream into a string to be returned */
                jsonString = readJSONInputStream(connection.getInputStream());
                if(jsonString == null) {
                    log.log(Level.SEVERE, "Failed to read connection input stream");
                }
                else {
                    log.log(Level.INFO, "jsonString: " + jsonString);
                }
            }
            else {
                log.log(Level.SEVERE, "Invalid response code [ " + responseCode + " ]");
            }

            return jsonString;
        }

These functions work on Android when the JSON is less than ~1000 bytes. But for a large JSON I have which is ~15000 it only reads ~half. As when I print the output I only see half of the JSON string.

Is there something I am doing wrong?

ant2009
  • 27,094
  • 154
  • 411
  • 609
  • follow this link u always send the large data into server use MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("avatar", new ByteArrayBody(data,"pic.jpg" )); http://stackoverflow.com/questions/14027882/try-to-upload-the-image-in-php-server-but-it-could-post-in-android – Rishi Gautam Nov 19 '15 at 09:15
  • I am not sending large data. But receiving a large JSON. – ant2009 Nov 19 '15 at 09:27

1 Answers1

1

There is no Error in the output, its just the android monitor (Logcat) have a limit of string length
that means your code is work perfectly

Edit ()

looks its not good to use string concatenation, it makes the performance so bad ,,,,
try to use

InputStream content = entity.getContent();

// (1)

StringBuilder builder =new StringBuilder()
BufferedReader bReader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = bReader.readLine()) != null) {
    builder.append(line);
}

And don't specify the size

S. Alawadi
  • 144
  • 1
  • 6
  • S. Alawadi. I don't think that is the case. As json string will be returned for parsing. The parser always fails because the JSON is invalid as half of it is missing. – ant2009 Nov 19 '15 at 09:19
  • S.Alawadi, thanks for the help. But the snippet didn't work for me. It failed to read anything. I have edited my questions with the code I use for the connection. entity.getContent() returns an object and not a inputstream. Am I correct? – ant2009 Nov 19 '15 at 11:46
  • can you read the json in the browser ,, if you can , then take the result and put it in Online JsonViewer , this way may help you to know if the json is already has invalid structure – S. Alawadi Nov 19 '15 at 12:45
  • Yes, I can retrieve the JSON using a firefox plugin. The JSON is valid when retrieved from the jave test case running on Linux. However, running on Android produces invalid as the JSON is not complete. Is there anyway to prove that the inputstream does contain all the JSON? And maybe it is a reading problem? – ant2009 Nov 19 '15 at 15:30
  • You was right about the logcat. That was the problem. – ant2009 Nov 20 '15 at 08:36