Why am I getting to Java.io.IOException stream closed?

Go To StackoverFlow.com

1

I am unsure of why I am getting this error. I tried searching for an answer on the net but was unsuccessful.

Below is the partial code where the error is taking place. I am sending a header and upon getting a successful response of 200 I will carry out the rest of my code.

URL url;
        HttpURLConnection connection = null;        
        try{


//Create connection

        url = new URL(targetURL);
        Log.i(TAG,"Connecting to : "+targetURL);
        connection = (HttpURLConnection)url.openConnection();

        //connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
                   "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Language", "en-US"); 
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");


        connection.setRequestProperty("x-device-displayheight", device_displayheight );

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuffer response = new StringBuffer();
            String rep = connection.getResponseMessage();
            int respcode = connection.getResponseCode();
            String line;

        try{                        
            if(respcode == 200 ){// Everything is ok                            
                if(is != null){
                    //response.append(is.toString());   

                    while((line = br.readLine()) != null){
                        Log.i("Output",line);
                    }

                }
                else{
                    Log.i("Nothing to parse","");
                }


            }
            else{// bad response from server
                Log.i("Bad Response", rep +" : "+respcode);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

//stack trace

04-04 13:14:41.668: W/System.err(23046): java.io.IOException: stream closed
04-04 13:14:41.668: W/System.err(23046):    at org.apache.harmony.luni.internal.net.www.protocol.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:69)
04-04 13:14:41.668: W/System.err(23046):    at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:41)
04-04 13:14:41.668: W/System.err(23046):    at java.io.InputStreamReader.read(InputStreamReader.java:248)
04-04 13:14:41.668: W/System.err(23046):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
04-04 13:14:41.668: W/System.err(23046):    at java.io.BufferedReader.readLine(BufferedReader.java:357)
04-04 13:14:41.668: W/System.err(23046):    at com.ameba.api.network.Client.executePost(Client.java:95)
04-04 13:14:41.668: W/System.err(23046):    at com.ameba.api.activityClasses.Login$connectToServer.doInBackground(Login.java:190)
04-04 13:14:41.672: W/System.err(23046):    at com.ameba.api.activityClasses.Login$connectToServer.doInBackground(Login.java:1)
04-04 13:14:41.672: W/System.err(23046):    at android.os.AsyncTask$2.call(AsyncTask.java:252)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
04-04 13:14:41.672: W/System.err(23046):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
04-04 13:14:41.672: W/System.err(23046):    at java.lang.Thread.run(Thread.java:1020)
2012-04-04 18:07
by Fabii
Please show us the stack trace - Steve Kuo 2012-04-04 18:13
@SteveKuo I just added the stack trac - Fabii 2012-04-04 18:17
call connection.connect(); before InputStream is = connection.getInputStream(); lin - ρяσѕρєя K 2012-04-04 18:33
Don't see anything functionally wrong with your code. Can you tell us a little about the env you are running in? proxies? firewalls? web-server - Tim Jones 2012-04-04 19:09
@imrankhan tried that, the problem still persists - Fabii 2012-04-04 20:59
@TimJones . No proxies , I am able to connect the web no problem and I am getting the correct response code. The issue seems to be with my InputStrea - Fabii 2012-04-04 21:00
@Fabii :thanks for fast respons - ρяσѕρєя K 2012-04-04 21:01
@Fabii :what error u are getting same or different - ρяσѕρєя K 2012-04-04 21:05
@imrankhan same error - Fabii 2012-04-04 21:17


1

System.setProperty("http.keepAlive", "false"); Seem to have solved the problem.  

2012-04-24 19:13
by Fabii


0

Try doing things in a different order and loop only if you have valid streams :

URL url;
        HttpURLConnection connection = null;        
        try{


//Create connection

        url = new URL(targetURL);
        Log.i(TAG,"Connecting to : "+targetURL);
        connection = (HttpURLConnection)url.openConnection();

        //connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
                   "application/x-www-form-urlencoded");
        connection.setRequestProperty("Content-Language", "en-US"); 
        connection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");


        connection.setRequestProperty("x-device-displayheight", device_displayheight );

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
                    InputStream is = connection.getInputStream();
            StringBuffer response = new StringBuffer();
            String rep = connection.getResponseMessage();
            int respcode = connection.getResponseCode();
            String line;
                    BufferedReader br = null;

        try{            
                            //use a constant below instead of 200           
            if(respcode == 200 ){// Everything is ok                            
                if(is != null){
                        br = new BufferedReader(new InputStreamReader(is));

                    //response.append(is.toString());   

                    while((line = br.readLine()) != null){
                        Log.i("Output",line);
                    }

                }
                else{
                    Log.i("Nothing to parse","");
                }


            }
            else{// bad response from server
                Log.i("Bad Response", rep +" : "+respcode);
            }
        }catch(Exception e){
            e.printStackTrace();
        } finally {
                      if( br != null ) { br.close() };
                    }
2012-04-04 18:35
by Snicolas
Ads