3

I'm having an issue with a JSON string in getting back from a FileInputStream in Java. I'm using Android. The problem is the string is not read in it's entirety and is cut off about half way through. I thought this might be to do with the max buffer size i have assigned maxBufferSize = 84 * 1024 in my UploadImage class, however if i increase that too much i get exception errors thrown when i attempt to read the byte stream. Here is my UploadImage class:

public class UploadImage {

public String serverUrl = "#";
public String filepath;
// Context allows for the access to application specific resources
Context context;

// json returned
public static String json_returned = "";

public UploadImage(String path, Context c) {
    filepath = path;context = c;
}

public int uploadFile() {

    String file = filepath;

    int serverResponse = 200;

    HttpURLConnection con = null;
    DataOutputStream dos = null;

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    // TODO: This size may differ greatly on different devices. Plz Fix.
    int maxBufferSize = 84 * 1024;

    File sourceFile = new File(file);
    Log.e("","SourceFile: "+sourceFile);
    // check if the file exists
    if(!sourceFile.isFile()) {
        return 0;
    } else {

        try {

            FileInputStream fileInput = new FileInputStream(sourceFile);

            URL url = new URL(serverUrl);

            // Open connection to send image
            con = (HttpURLConnection)url.openConnection();
            con.setDoInput(true); // Allow Inputs
            con.setDoOutput(true); // Allow Outputs
            con.setUseCaches(false);
            con.setRequestMethod("POST");
            con.setRequestProperty("Connection", "Keep-Alive");
            con.setRequestProperty("ENCTYPE", "multipart/form-data");
            con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
            con.setRequestProperty("image_data", filepath);

            // Now get our response/output stream from the server
            dos = new DataOutputStream(con.getOutputStream());
            dos.writeBytes("--*****\r\n");
            dos.writeBytes("Content-Disposition: form-data; name='image_data';filename="+"'"
                    + filepath + ".jpg'" + "\r\n");
            dos.writeBytes("\r\n");

            // Now read our file and then write to it
            bytesAvailable = fileInput.available();
            Log.e("", "BytesAvailable: "+bytesAvailable);
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = fileInput.read(buffer, 0, maxBufferSize);

            while(bytesRead > 0) {
                dos.write(buffer,0,bufferSize);
                bytesAvailable = fileInput.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInput.read(buffer,0,bufferSize);
            }

            dos.writeBytes("\r\n");
            dos.writeBytes("--*****\r\n");

            BufferedReader sr = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            StringBuilder responseString = new StringBuilder();

            String inputStr;
            while((inputStr = sr.readLine()) != null) {
                responseString.append(inputStr);
            }

            GetResponse(responseString.toString());

            // get server response
            serverResponse = con.getResponseCode();
            String response = con.getResponseMessage();

            Log.i("image_data", "HTTP Response is : "
                    + response + ": " + serverResponse);


            // close the stream
            fileInput.close();
            dos.flush();
            dos.close();

            Log.e("End of Connection, ", "File apparently Uploaded.");

        } catch(MalformedURLException ex) {
            Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
        } catch(Exception e) {
            //Log.e("Other Exception: ", "ex: "+e);
            e.printStackTrace();
        }

    }
    return serverResponse;
}

public void GetResponse(String response) {
    if(!response.isEmpty()) {
        this.json_returned = response;
    }
}

public String getString() {
    return json_returned;
}



}

The JSON string i should be expecting is this:

{  
       "foods":{  
          "food":[  
             {  
                "food_description":"Per 101g - Calories: 197kcal | Fat: 7.79g | Carbs: 0.00g | Protein: 29.80g",
                "food_id":"1641",
                "food_name":"Chicken Breast",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-ns-as-to-skin-eaten"
             },
             {  
                "food_description":"Per 100g - Calories: 110kcal | Fat: 1.24g | Carbs: 0.00g | Protein: 23.09g",
                "food_id":"4881229",
                "food_name":"Skinless Chicken Breast",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-skinless"
             },
             {  
                "food_description":"Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g",
                "food_id":"448901",
                "food_name":"Grilled Chicken",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-grilled-ns-as-to-skin-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 247kcal | Fat: 15.49g | Carbs: 0.00g | Protein: 25.06g",
                "food_id":"1695",
                "food_name":"Chicken Thigh",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-thigh-ns-as-to-skin-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g",
                "food_id":"419178",
                "food_name":"Rotisserie Chicken",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-rotisserie-ns-as-to-skin-eaten"
             },
             {  
                "brand_name":"Valbest",
                "food_description":"Per 4 oz - Calories: 130kcal | Fat: 3.00g | Carbs: 0.00g | Protein: 26.00g",
                "food_id":"3946778",
                "food_name":"Chicken Breast",
                "food_type":"Brand",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/valbest\/chicken-breast"
             },
             {  
                "food_description":"Per 101g - Calories: 216kcal | Fat: 11.15g | Carbs: 0.00g | Protein: 27.03g",
                "food_id":"1677",
                "food_name":"Chicken Drumstick",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-drumstick-ns-as-to-skin-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 190kcal | Fat: 7.41g | Carbs: 0.00g | Protein: 28.93g",
                "food_id":"1628",
                "food_name":"Roasted Broiled or Baked Chicken (Skin Not Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-roasted-broiled-or-baked-skin-not-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g",
                "food_id":"1623",
                "food_name":"Chicken",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-ns-as-to-skin-eaten"
             },
             {  
                "brand_name":"Cub Foods",
                "food_description":"Per 1 small - Calories: 110kcal | Fat: 2.50g | Carbs: 0.00g | Protein: 21.00g",
                "food_id":"26245",
                "food_name":"Boneless Skinless Chicken Breast",
                "food_type":"Brand",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/cub-foods\/boneless-skinless-chicken-breast"
             },
             {  
                "food_description":"Per 96g - Calories: 279kcal | Fat: 16.60g | Carbs: 9.59g | Protein: 21.45g",
                "food_id":"1636",
                "food_name":"Baked or Fried Coated Chicken with Skin (Skin\/Coating Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-coated-baked-or-fried-prepared-with-skin-skin-coating-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 209kcal | Fat: 10.88g | Carbs: 0.00g | Protein: 25.94g",
                "food_id":"1697",
                "food_name":"Chicken Thigh (Skin Not Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-thigh-skin-not-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 290kcal | Fat: 19.46g | Carbs: 0.00g | Protein: 26.86g",
                "food_id":"1713",
                "food_name":"Chicken Wing",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-wing-ns-as-to-skin-eaten"
             },
             {  
                "brand_name":"Pilgrim\u0027s Pride",
                "food_description":"Per 1 serving - Calories: 90kcal | Fat: 0.00g | Carbs: 0.00g | Protein: 22.00g",
                "food_id":"25945",
                "food_name":"Chicken Breast Tenderloins",
                "food_type":"Brand",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/pilgrims-pride\/chicken-breast-tenderloins"
             },
             {  
                "food_description":"Per 103g - Calories: 241kcal | Fat: 10.17g | Carbs: 9.97g | Protein: 25.73g",
                "food_id":"1657",
                "food_name":"Baked or Fried Coated Chicken Breast Skinless (Coating Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-coated-baked-or-fried-prepared-skinless-coating-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 247kcal | Fat: 15.49g | Carbs: 0.00g | Protein: 25.06g",
                "food_id":"1696",
                "food_name":"Chicken Thigh (Skin Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-thigh-skin-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 165kcal | Fat: 3.57g | Carbs: 0.00g | Protein: 31.02g",
                "food_id":"1643",
                "food_name":"Chicken Breast (Skin Not Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-skin-not-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 190kcal | Fat: 7.41g | Carbs: 0.00g | Protein: 28.93g",
                "food_id":"448917",
                "food_name":"Grilled Chicken (Skin Not Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-grilled-skin-not-eaten"
             },
             {  
                "food_description":"Per 101g - Calories: 232kcal | Fat: 13.46g | Carbs: 0.00g | Protein: 25.96g",
                "food_id":"1660",
                "food_name":"Chicken Leg (Skin Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-leg-(drumstick-and-thigh)-skin-eaten"
             },
             {  
                "food_description":"Per 104g - Calories: 221kcal | Fat: 9.52g | Carbs: 0.00g | Protein: 31.92g",
                "food_id":"1637",
                "food_name":"Baked or Fried Coated Chicken with Skin (Skin\/Coating Not Eaten)",
                "food_type":"Generic",
                "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-coated-baked-or-fried-prepared-with-skin-skin-coating-not-eaten"
             }
          ],
          "max_results":"20",
          "page_number":"0",
          "total_results":"4726"
       }
    }

However i am only getting back this:

{ "foods": { "food": [ {"food_description": "Per 101g - Calories: 197kcal | Fat: 7.79g | Carbs: 0.00g | Protein: 29.80g", "food_id": "1641", "food_name": "Chicken Breast", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-ns-as-to-skin-eaten" }, {"food_description": "Per 100g - Calories: 110kcal | Fat: 1.24g | Carbs: 0.00g | Protein: 23.09g", "food_id": "4881229", "food_name": "Skinless Chicken Breast", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-breast-skinless" }, {"food_description": "Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g", "food_id": "448901", "food_name": "Grilled Chicken", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-grilled-ns-as-to-skin-eaten" }, {"food_description": "Per 101g - Calories: 247kcal | Fat: 15.49g | Carbs: 0.00g | Protein: 25.06g", "food_id": "1695", "food_name": "Chicken Thigh", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-thigh-ns-as-to-skin-eaten" }, {"food_description": "Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g", "food_id": "419178", "food_name": "Rotisserie Chicken", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-rotisserie-ns-as-to-skin-eaten" }, {"brand_name": "Valbest", "food_description": "Per 4 oz - Calories: 130kcal | Fat: 3.00g | Carbs: 0.00g | Protein: 26.00g", "food_id": "3946778", "food_name": "Chicken Breast", "food_type": "Brand", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/valbest\/chicken-breast" }, {"food_description": "Per 101g - Calories: 216kcal | Fat: 11.15g | Carbs: 0.00g | Protein: 27.03g", "food_id": "1677", "food_name": "Chicken Drumstick", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-drumstick-ns-as-to-skin-eaten" }, {"food_description": "Per 101g - Calories: 190kcal | Fat: 7.41g | Carbs: 0.00g | Protein: 28.93g", "food_id": "1628", "food_name": "Roasted Broiled or Baked Chicken (Skin Not Eaten)", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-roasted-broiled-or-baked-skin-not-eaten" }, {"food_description": "Per 101g - Calories: 239kcal | Fat: 13.60g | Carbs: 0.00g | Protein: 27.30g", "food_id": "1623", "food_name": "Chicken", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-ns-as-to-skin-eaten" }, {"brand_name": "Cub Foods", "food_description": "Per 1 small - Calories: 110kcal | Fat: 2.50g | Carbs: 0.00g | Protein: 21.00g", "food_id": "26245", "food_name": "Boneless Skinless Chicken Breast", "food_type": "Brand", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/cub-foods\/boneless-skinless-chicken-breast" }, {"food_description": "Per 96g - Calories: 279kcal | Fat: 16.60g | Carbs: 9.59g | Protein: 21.45g", "food_id": "1636", "food_name": "Baked or Fried Coated Chicken with Skin (Skin\/Coating Eaten)", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-coated-baked-or-fried-prepared-with-skin-skin-coating-eaten" }, {"food_description": "Per 101g - Calories: 209kcal | Fat: 10.88g | Carbs: 0.00g | Protein: 25.94g", "food_id": "1697", "food_name": "Chicken Thigh (Skin Not Eaten)", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-thigh-skin-not-eaten" }, {"food_description": "Per 101g - Calories: 290kcal | Fat: 19.46g | Carbs: 0.00g | Protein: 26.86g", "food_id": "1713", "food_name": "Chicken Wing", "food_type": "Generic", "food_url": "http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/chicken-wing-ns-as-to-skin-eaten" }, {"brand_name": "Pilgrim\u0027s Pride", "food_description": "Per 1 serving - Calories: 90kcal | Fat: 0.00g | Carbs: 0.00g | Protein: 22.00g", "foo
Zy0n
  • 810
  • 2
  • 14
  • 33
  • why not use a simpler method to get the JSON? http://stackoverflow.com/a/13267720/1742597 – fakataha Mar 24 '15 at 02:52
  • Thanks, I just tried this. Unfortunately i am still having the same issue. The string seems to be cut of toward the end? – Zy0n Mar 24 '15 at 03:00
  • Does the method finish cleanly, i.e. no exceptions? – fakataha Mar 24 '15 at 03:18
  • No exceptions apart from when i come to pass the string as a `JSONObject`. But that's only because it's an incomplete JSON string. – Zy0n Mar 24 '15 at 03:20
  • 1
    Logcat has a maximum: is this just something you are seeing in logcat? – Ashton Engberg Mar 24 '15 at 03:26
  • Yes, I'm looking at it there. I didn't realize there was a limit to logcat. I just output the string 'as is' into the textview to test and it's still being cut off. – Zy0n Mar 24 '15 at 03:32
  • Did you ever find a solution to this problem? – Edmund Rojas Oct 29 '15 at 00:24
  • You are misusing `available()` here, and there is no need for this convoluted copy code. Have a search here for 'standard Java copy loop'. You'll be surprised. – user207421 Sep 18 '19 at 00:57

1 Answers1

0

Found a good workaround in this post thanks to the comment above pointing out that Logcat has a maximum file size for single log entries. Instead of Log.d("tag", "msg"), use this function which will break it up into smaller strings for Logcat:

public static void largeLog(String tag, String content) {
   if (content.length() > 4000) {
       Log.d(tag, content.substring(0, 4000));
       largeLog(tag, content.substring(4000));
   } else {
       Log.d(tag, content);
   }
}
A. Kendall
  • 391
  • 3
  • 12