我有一個Android應用程序,讓我這個例外:的Java,MalformedChunkCodingException
org.apache.http.MalformedChunkCodingException: CRLF expected at end of chunk
唯一的例外是從此方法拋出:(目的是寫出來的,從服務器到一個文件中收到的響應)
public static void getResponseBodyForServerData(
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;
}
File file = new File(Environment.getExternalStorageDirectory() +
"/foo/Response.txt");
if (!file.exists()) {
file.createNewFile();
}
OutputStream out = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len;
while ((len = instream.read(buf)) > 0)
out.write(buf, 0, len);
out.flush();
out.close();
}
於是我修改上面的代碼:
public static void getResponseBodyForServerData(
final HttpEntity entity) throws IOException, ParseException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream instream = entity.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
instream, "UNICODE") ;
if (instream == null) {
return;
}
File file = new File(Environment.getExternalStorageDirectory() +
"/foo/Response.txt");
if (!file.exists()) {
file.createNewFile();
}
BufferedReader bufferedReader = new BufferedReader(inputStreamReader) ;
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file),"UNICODE")) ;
String singleLine = null ;
while((singleLine = bufferedReader.readLine()) != null) {
bufferedWriter.write(singleLine) ;
}
bufferedWriter.flush() ;
bufferedWriter.close() ;
}
而且噸該代碼的作品,是什麼導致了這個錯誤和原代碼有什麼問題?
你是通過代理加載?因爲每次我看到這個異常,人們都通過代理加載,並通過刪除緩衝區大小或更改它來糾正錯誤。 – Sephy 2011-12-26 11:11:37
你在說網絡代理嗎? – Vaibhav 2011-12-26 11:20:59