產生的緣由,應該是服務器返回的數據不是規範的格式 。
我使用的xutils的httpUtils來實現網絡通訊的,關於讀取數據是在json
StringDownloadHandler類中源代碼服務器
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
current += OtherUtils.sizeOfString(line, charset);
if (callBackHandler != null) {
if (!callBackHandler.updateProgress(total, current, false)) {
break;
}
}
}
這樣的實現,並不夠健壯。
使用二進制的讀取更加穩健網絡
inputStream = entity.getContent();
byte data[] = new byte[40000];
long totalContactsCount = -1;
int readContactsCount = 0;
int currentByteReadCount = 0;
while ((currentByteReadCount = inputStream.read(data)) != -1) {
String readData = new String(data, 0, currentByteReadCount);
sb.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));
}*/
}
參考app
http://stackoverflow.com/questions/19486656/getting-malformedchunkcodingexception-while-reading-json-dataorm