Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON字符串中途中断,java inputstream_Java_Android_Json - Fatal编程技术网

JSON字符串中途中断,java inputstream

JSON字符串中途中断,java inputstream,java,android,json,Java,Android,Json,我在从Java中的FileInputStream返回时遇到了一个JSON字符串问题。我用的是安卓系统。问题是字符串没有被完整地读取,并且在大约一半的时候被截断。我认为这可能与我在UploadImage类中分配的最大缓冲区大小有关,但是如果我增加太多,我会在尝试读取字节流时引发异常错误。这是我的UploadImage类: public class UploadImage { public String serverUrl = "#"; public String filepath; // Con

我在从Java中的
FileInputStream
返回时遇到了一个JSON字符串问题。我用的是安卓系统。问题是字符串没有被完整地读取,并且在大约一半的时候被截断。我认为这可能与我在
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

在中找到了一个很好的解决方法,这要感谢上面的评论,它指出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);
   }
}

为什么不使用更简单的方法来获取JSON呢?谢谢,我刚试过这个。不幸的是,我仍然有同样的问题。字符串似乎在接近尾端时被切掉了?方法是否干净地完成,即没有异常?除了作为
JSONObject
传递字符串时没有异常。但这只是因为它是一个不完整的JSON字符串。Logcat有一个最大值:这就是您在Logcat中看到的东西吗?