更新:無論我嘗試接收多少數據,我都會得到相同的錯誤。任何想法和建議,可以幫助我解決這個問題將不勝感激。獲取「流關閉」IOException當請求相當長的響應時
我在嘗試從Web服務中成員列表中檢索UserID列表的過程。不幸的是,在收到只有幾個成員之後,我得到了「Stream is Closed」IOException。這是我第一次嘗試檢索這種數量的流(我們正在談論+100個成員),也是我第一次收到此錯誤。
有問題的錯誤:
其中發生異常的代碼:
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;
}
什麼造成問題的任何想法?我獲得的數據量是否有限制?
沒有。我從來沒有接受gzip編碼。我想也許是這個問題,但在我的所有數據被檢索之前,數據流仍然關閉。 – CodePrimate