Java和json响应EOFException

时间:2012-06-01 10:26:35

标签: android json web-services request response

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)

1 个答案:

答案 0 :(得分:0)

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

Connection.setRequestProperty( "Accept-Encoding", "" ); add this line of code before use the connection