获取“流已关闭”IOException请求相当长的响应

时间:2012-06-25 12:01:56

标签: java android gzip ioexception

更新:无论我尝试接收的数据有多少,我都知道得到同样的错误。任何可以帮助我解决这个问题的想法和建议都将不胜感激。

我正在尝试从Web服务中的成员列表中检索UserID列表。不幸的是,在收到少数会员后,我收到了“Stream is Closed”IOException。这是我第一次尝试检索这么大的流(我们说的是+100名成员),也是我第一次收到这个错误。

有问题的错误:

IOException

发生异常的代码:

private void setupRequest(OAuthRequest request) 
{
    request.setConnectionKeepAlive(true);
    request.setConnectTimeout(60, TimeUnit.SECONDS);
    request.addHeader("accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*");
//  request.addHeader("accept-encoding", "gzip, deflate");
    request.addHeader("user-agent", m_sIDENTIFIER);
}

private String readStream(InputStream stream) throws IOException 
{
    String sReturn = "";
    if(stream != null)
    {
        final BufferedReader bufReader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
        final StringBuffer s2 = new StringBuffer();
        String line = bufReader.readLine();
        if(line != null)
        {
            s2.append(line);
            while((line = bufReader.readLine()) != null)
            {
                s2.append('\n');
                s2.append(line);
            }
        }
        bufReader.close();
        sReturn = s2.toString();
    }
    return sReturn;
}

private InputStream getResultStream(Response response) throws IOException
{
    InputStream resultStream = null;
    if(response != null)
    {
        String encoding = response.getHeader("Content-Encoding");
        if((encoding != null) && (encoding.equalsIgnoreCase("gzip")))
        {
            Log.d("Stream :", "Read GZIP");

        } else if ((encoding != null) && encoding.equalsIgnoreCase("deflate")) {
            resultStream = new InflaterInputStream(response.getStream(), new Inflater(true));
            Log.d("Stream :", "Read Deflated.");
        } else {
            resultStream = response.getStream();
            Log.d("Stream :","Read Normal.");
        }       
    }

    return resultStream;
}

对导致问题的原因有什么看法?我收到多少数据是否有限制?

2 个答案:

答案 0 :(得分:0)

我认为您的连接有时间。

request.setConnectTimeout(60, TimeUnit.SECONDS);

增加秒数

答案 1 :(得分:0)

我会说远程服务器关闭连接(由于错误?)。从您的日志中我可以看到响应被gzip压缩。您的成功回复也是如此吗?