1

我在连接时遇到了很大的问题json webservice。我想gzipped从服务器获取数据。下面是我的代码。问题是当我解析响应时,我正在接受gzip编码并且正在获取gzipped数据,我逐行读取此数据,一切看起来都很好,然后application throw EOFException,出了什么问题?如果我可以读取部分gzipped数据,我认为服务器响应很好。我会感谢任何帮助:)。

    HttpPost httppost = new HttpPost(requestUrl);
    httppost.addHeader("Accept-Encoding", "gzip");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    inputStream = entity.getContent();
    contentEncoding = response.getFirstHeader("Content-Encoding");
}

private String convertResponseToString() throws IOException {
    GZIPInputStream zis = null;

    if (contentEncoding != null
            && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        GZIPInputStream gzipStream = new GZIPInputStream(inputStream);

        if (true) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[256];
            int len;
            while ((len = gzipStream.read(buf, 0, 256)) > 0) {
                bos.write(buf, 0, len);
            }
            bos.flush();
            buf = bos.toByteArray();
            inputStream.close();
            inputStream = new ByteArrayInputStream(buf);
        }

    }

异常堆栈跟踪:

06-01 10:56:25.447: W/System.err(285): java.io.EOFException
06-01 10:56:25.476: W/System.err(285):  at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:219)
06-01 10:56:25.476: W/System.err(285):  at java.util.zip.GZIPInputStream.verifyCrc(GZIPInputStream.java:202)
06-01 10:56:25.476: W/System.err(285):  at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:186)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.requests.RequestSender.convertResponseToString(RequestSender.java:79)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.requests.RequestSender.parseToJSON(RequestSender.java:108)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.requests.GetConferencesListRequestSender.getResults(GetConferencesListRequestSender.java:36)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.requests.GetConferencesListRequestSender.getResults(GetConferencesListRequestSender.java:1)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.requests.RequestExecutor.go(RequestExecutor.java:33)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.EventListView.containerConferences(EventListView.java:138)
06-01 10:56:25.476: W/System.err(285):  at org.pw.inf.EventListView.onClick(EventListView.java:101)
06-01 10:56:25.476: W/System.err(285):  at android.view.View.performClick(View.java:2364)
06-01 10:56:25.476: W/System.err(285):  at android.view.View.onTouchEvent(View.java:4179)
06-01 10:56:25.476: W/System.err(285):  at android.view.View.dispatchTouchEvent(View.java:3709)
06-01 10:56:25.476: W/System.err(285):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-01 10:56:25.476: W/System.err(285):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-01 10:56:25.486: W/System.err(285):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-01 10:56:25.486: W/System.err(285):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-01 10:56:25.486: W/System.err(285):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-01 10:56:25.486: W/System.err(285):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
06-01 10:56:25.486: W/System.err(285):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
06-01 10:56:25.486: W/System.err(285):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
06-01 10:56:25.486: W/System.err(285):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
06-01 10:56:25.486: W/System.err(285):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-01 10:56:25.486: W/System.err(285):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-01 10:56:25.486: W/System.err(285):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 10:56:25.486: W/System.err(285):  at android.os.Looper.loop(Looper.java:123)
06-01 10:56:25.486: W/System.err(285):  at android.app.ActivityThread.main(ActivityThread.java:4363)
06-01 10:56:25.486: W/System.err(285):  at java.lang.reflect.Method.invokeNative(Native Method)
06-01 10:56:25.486: W/System.err(285):  at java.lang.reflect.Method.invoke(Method.java:521)
06-01 10:56:25.486: W/System.err(285):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-01 10:56:25.486: W/System.err(285):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-01 10:56:25.486: W/System.err(285):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

https://code.google.com/p/android/issues/detail?id=24672

Connection.setRequestProperty( "Accept-Encoding", "" ); add this line of code before use the connection
于 2016-12-20T06:26:15.703 回答