gpt4 book ai didi

java - JSON字符串中途截断,java输入流

转载 作者:太空狗 更新时间:2023-10-29 14:58:29 25 4
gpt4 key购买 nike

我在从 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

最佳答案

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

关于java - JSON字符串中途截断,java输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29223980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com