2012-05-08 41 views
4

雖然我使用GZIPInputStream壓縮字節從網上獲得,程序運行錯誤,如下所示:GZIPInputStream拋出未知格式(幻數213C)

05-08 17:37:02.465: W/System.err(744): java.io.IOException: unknown format (magic number 213c) 
05-08 17:37:02.465: W/System.err(744): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:84) 
05-08 17:37:02.465: W/System.err(744): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:64) 
05-08 17:37:02.475: W/System.err(744): at com.Android.Sample.TestActivity.onCreate(TestActivity.java:54) 
05-08 17:37:02.475: W/System.err(744): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
05-08 17:37:02.475: W/System.err(744): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-08 17:37:02.475: W/System.err(744): at android.os.Looper.loop(Looper.java:123) 
05-08 17:37:02.475: W/System.err(744): at android.app.ActivityThread.main(ActivityThread.java:3683) 
05-08 17:37:02.475: W/System.err(744): at java.lang.reflect.Method.invokeNative(Native Method) 
05-08 17:37:02.475: W/System.err(744): at java.lang.reflect.Method.invoke(Method.java:507) 
05-08 17:37:02.475: W/System.err(744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
05-08 17:37:02.475: W/System.err(744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
05-08 17:37:02.486: W/System.err(744): at dalvik.system.NativeStart.main(Native Method) 

和我的代碼如下:

HttpGet request = new HttpGet("http://www.google.com"); 
HttpResponse response = new DefaultHttpClient().execute(request); 
HttpEntity entity = response.getEntity(); 
InputStream stream = entity.getContent(); 
InputStream zippedStream = new GZIPInputStream(stream); 
InputStreamReader reader = new InputStreamReader(zippedStream); 
BufferedReader buffer = new BufferedReader(reader); 

如果我只是使用

InputStreamReader reader = new InputStreamReader(stream); 

不壓縮流,它會好的。

回答

3

你確定你得到的是壓縮的嗎?如果你獲得'未知格式',那肯定不是。您應該檢查響應標題並相應地啓用GZIP。

9

在接收輸入流之前,您必須檢查內容編碼。如果響應在GZIP中編碼,請使用GZIPInputStream。這是我的解決方案,我在我的API call中使用。

InputStream responseStream = null; 
... 
// receive response 
String encoding = connection.getHeaderField("Content-Encoding"); 
boolean gzipped = encoding!=null && encoding.toLowerCase().contains("gzip"); 
try 
{ 
    InputStream inputStream = connection.getInputStream(); 
    if(gzipped) responseStream = new BufferedInputStream(new GZIPInputStream(inputStream)); 
    else responseStream = new BufferedInputStream(inputStream); 
} 
catch(FileNotFoundException e) 
{ 
    // error stream 
    InputStream errorStream = connection.getErrorStream(); 
    if(gzipped) responseStream = new BufferedInputStream(new GZIPInputStream(errorStream)); 
    else responseStream = new BufferedInputStream(errorStream); 
} 
... 
// TODO: close stream