JSON字符串中断一半,java inputstream [英] JSON string cut off half way, java inputstream

查看:118
本文介绍了JSON字符串中断一半,java inputstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java中的FileInputStream取回时,JSON字符串出现问题.我正在使用Android.问题是字符串没有被完整读取,并且被截断了大约一半.我以为这可能与我在UploadImage类中分配的maxBufferSize = 84 * 1024的最大缓冲区大小有关,但是,如果我增加太多,尝试读取字节流时会抛出异常错误.这是我的UploadImage类:

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



}

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

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

但是我只得到这个:

{ "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的较小字符串:

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

这篇关于JSON字符串中断一半,java inputstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆