Android 读取JSON数据时获取格式错误的ChunkCodingException
我在解析JSON数据时遇到此异常: org.apache.http.MalformedChunkCodingException:分块流意外结束 位于org.apache.http.impl.io.ChunkedInputStream.getChunkSize 有人能给我建议怎么做吗…我正在阅读流作为:Android 读取JSON数据时获取格式错误的ChunkCodingException,android,json,parsing,streamreader,Android,Json,Parsing,Streamreader,我在解析JSON数据时遇到此异常: org.apache.http.MalformedChunkCodingException:分块流意外结束 位于org.apache.http.impl.io.ChunkedInputStream.getChunkSize 有人能给我建议怎么做吗…我正在阅读流作为: HttpPost request = new HttpPost(url); StringBuilder sb=new StringBuilder();
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 String getResponseBody(final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
if (instream == null) {
return "";
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
"HTTP entity too large to be buffered in memory");
}
StringBuilder buffer = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8));
String line = null;
try {
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
} finally {
instream.close();
reader.close();
}
return buffer.toString();
}
如何使用?
HttpResponse WSresponse = httpclient.execute(httppost);
String response = getResponseBody(WSresponse.getEntity());
试试这个我从这个方法中找到了解决方案
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();
}
Thanx为了快速响应,我尝试了您的解决方案,但在reader.readline()上出现了相同的异常。我也遇到了这个错误,并且很难找到解决方案。是否有已知的解决方法?将resultString转换为JSONArray并找出其中的JSONObject.:)@Droid我在reader.readline()处遇到此异常,因此我无法获取结果字符串。您是否异步建立连接?
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();
}