gpt4 book ai didi

android - 读取 JSON 数据时出现 MalformedChunkCodingException

转载 作者:行者123 更新时间:2023-11-29 00:25:56 24 4
gpt4 key购买 nike

我在解析 JSON 数据时遇到这个异常:

org.apache.http.MalformedChunkCodingException:分块流意外结束

在 org.apache.http.impl.io.ChunkedInputStream.getChunkSize

任何人都可以建议我做什么......我正在阅读 stream As :

HttpPost request = new HttpPost(url);
StringBuilder sb=new StringBuilder();
request.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
request.setHeader("Accept", "application/json");
HttpResponse response =null;
DefaultHttpClient httpClient = new DefaultHttpClient();
//DefaultHttpClient httpClient = getNewHttpClient();
HttpConnectionParams.setSoTimeout(httpClient.getParams(), timeOut);
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),timeOut);
response = httpClient.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = reader.readLine()) != null){
sb.append(line);
}
resultString = sb.toString();

最佳答案

我从这个方法中找到了解决方案

public static String getResponseStringFromURL(String url,int timeOut)
{
StringBuilder result = new StringBuilder();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
HttpResponse response =null;

try {
response = httpClient.execute(request);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


HttpEntity entity = response.getEntity();
InputStream input = null;
try {
input = new BufferedInputStream(response.getEntity().getContent());
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte data[] = new byte[40000];

long totalContactsCount = -1;
int readContactsCount = 0;
int currentByteReadCount = 0;

/** read response from inpus stream */
try {
while ((currentByteReadCount = input.read(data)) != -1) {
String readData = new String(data, 0, currentByteReadCount);
result.append(readData);

// then +1 progress on every ...},{... (JSON object separator)
if (readData.indexOf("}~{") >= 0) {
readContactsCount++;
}

/* // publishing the progress....
if (totalContactsCount > 0) {
publishProgress((int)(readContactsCount * 100 / totalContactsCount));
}*/
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/** transform response into JSONArray */
return result.toString();

}

关于android - 读取 JSON 数据时出现 MalformedChunkCodingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19486656/

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