JSON字符串切断了一半,java输入流

时间:2015-03-24 02:26:18

标签: java android json

我在使用Java中的FileInputStream取回JSON字符串时遇到问题。我正在使用Android。问题是字符串不是完全读取的,而是在中途被删除。我认为这可能与我在maxBufferSize = 84 * 1024类中分配UploadImage的最大缓冲区大小有关,但是如果我增加太多,我会在尝试读取字节流时抛出异常错误。这是我的UploadImage类:

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;
}



}

我应该期待的JSON字符串是:

{  
       "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"
       }
    }

但是我只是回来了:

{ "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

1 个答案:

答案 0 :(得分:0)

this post中找到了一个很好的解决方法,这要感谢上面的评论指出Logcat具有单个日志条目的最大文件大小。代替此Log.d(“ tag”,“ msg”),使用此函数会将其分解为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);
   }
}