0

我正在尝试为 Android 构建 3G 连接速度测试

我被 Logcat 中显示的 IOException 卡住了:

“IOException:BufferedInputStream 已关闭”

这是代码:

   OutputStream output = null;
    InputStream input = null;
    try{  
           URL url = new URL(myFullPathUrl);
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
           urlConnection.setDoInput(true);
           urlConnection.setDoOutput(true);


           output = urlConnection.getOutputStream();
           input = urlConnection.getInputStream();

    //note: the guy who worked on this project before me use socket, and he requested the file like this:
    //          output.write( ("GET " + filePath + " HTTP/1.0\r\n").getBytes() );
    //          output.write("Accept: */*\r\n".getBytes());
    //          output.write("\r\n".getBytes());
    // where filePath is something like "/directory/image.jpg"
    // and when he open the socket connecion, he use only host name like "my.domain.com"
    // But I changed his code to use HttpURLConnection instead and use myFullPathUrl =            
    // "my.domain.com/directory/image.jpg"

        catch (Exception e){
             e.printStackTrace();
        }finally {
        urlConnection.disconnect(); //edit 3 : this is the culprit. Sorry I forgot to include this. Please all accept my apology and let's see this question as my little contribution that you can't use the input/output stream object after you call .disconnect()
             }  
while ((DownloadedByte += input.read(BufferData,0,BufferData.length)) != -1)//*** Exception at this line
            {.......... //Connection speed test code here

根据 Logcat,异常发生在带有 while() 循环的行上(我在上面标记了*

我已经确认 inputStream 对象不为空。任何人都知道可能是什么原因?

谢谢!

最好的,基蒂

编辑:这是所要求的完整 Logcat:

07-30 15:58:00.349: W/System.err(26626): java.io.IOException: BufferedInputStream is closed
07-30 15:58:00.349: W/System.err(26626):    at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:118)
07-30 15:58:00.359: W/System.err(26626):    at java.io.BufferedInputStream.read(BufferedInputStream.java:271)
07-30 15:58:00.359: W/System.err(26626):    at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
07-30 15:58:00.359: W/System.err(26626):    at com.mycompany.speedtest.util.MyHttpDownloader.doInBackground(DownloadFileSocket.java:202)
07-30 15:58:00.359: W/System.err(26626):    at com.mycompany.speedtest.util.MyHttpDownloader.doInBackground(DownloadFileSocket.java:1)
07-30 15:58:00.359: W/System.err(26626):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-30 15:58:00.359: W/System.err(26626):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-30 15:58:00.359: W/System.err(26626):    at java.lang.Thread.run(Thread.java:864)

编辑2:

我只是尝试调用 input.available() 来获取可以读取的估计字节数,并且得到了相同的异常... -*-

4

4 回答 4

2

您在 while 循环中的条件不正确。InputStream#read返回读取的字节数,如果在流的末尾则返回 -1。但是,您不断将读取的字节数添加到DownloadedByte. 请改用以下条件(注意 = 而不是 +=):

while ( ( bytesRead = inputStream.read( result, 0, result.length ) ) != -1 ) {
   // TODO
}
于 2013-07-30T08:16:49.087 回答
1

罪魁祸首是 urlConnection.disconnect();

删除它,问题就解决了。请参阅问题中的“编辑 3”。谢谢大家!!=)

于 2013-07-30T09:59:52.260 回答
1

我建议您将 while 循环的代码放入 try 块中。因为只有在没有抛出异常的情况下才能处理这段代码。

如果你能发布你得到的整个例外,那就太好了

最好的祝愿

于 2013-07-30T08:18:57.593 回答
-2

尝试设置超时:

urlConnection.setConnectTimeout(10000); //set timeout to 10 seconds
于 2013-07-30T08:13:36.907 回答