I'm trying to build a 3G connection speed test for Android
I got stuck with the IOException being shown in the Logcat:
"IOException : BufferedInputStream is closed"
Here's the code:
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
According to Logcat, the Exception happen on the line with while() loop (I marked * above)
I've confirmed that the inputStream object is not null. Anyone have an idea what could be the cause?
Thanks!
Best, Kitti
edit: Here's the full Logcat as requested:
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)
edit 2:
I just tried calling input.available() to get an estimate number of bytes that can be read, and just got the same Exception... -*-