Search code examples
loopsandroid-asynctaskioexception

java.io.IOException: closed error in a non-stop loop inside an asynctask


I try to create an asynctask that runs permanently (all the time my app is running). Thay task read every second a file on a server (status.xml).

My problem is that when I execute the app, I have an java.io.IOException: closed exception the second time I do :

  reader.read(buffer);  // HERE I HAVE AN IOException closed

(first loop is ok, then I have error each loop)

Thanks if someone can help me. I undesrtand the reason of the error, but I cannot find a solution...

Here is my code :

class StatusnAsync extends AsyncTask<Void, Void, Void> {
    InputStream in = null;
    int responseCode;

    void Sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPreExecute() {
     // inits for doInBackground thread
        try {
            URL url = new URL(address + "/status.xml");
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(5000        /* milliseconds */);
            conn.setConnectTimeout(80000    /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        while (not_end) {
            try {
                readStatus();
                // Sleep 1 sec
                Sleep(1000);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
        return null;
    }

    private void readStatus() throws IOException {
        try {
            conn.connect();
            responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = conn.getInputStream();
                // Convert the InputStream into a string
                String contentAsString = readIt(in, 340);
                // close the inputstream
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            if (in != null) in.close();
        }
    }

    // Reads an InputStream and converts it to a String.
    public String readIt(InputStream stream, int len) throws IOException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[len];
        reader.read(buffer);            // HERE I HAVE AN IOException closed
        return new String(buffer);
    }

}

Thank you.


Solution

  • Sorry for my question, I found my error, a stupid error. Of course I need to openConnection for each GET.

    I give the corrected code if it can help someone :

    class StatusnAsync extends AsyncTask<Void, Void, Void> {
    InputStream in = null;
    int responseCode;
    URL url;
    
    void Sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected void onPreExecute() {
     // inits for doInBackground thread
        try {
            url = new URL(address + "/file.xml");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected Void doInBackground(Void... arg0) {
        while (not_end) {
            try {
                readStatus();
                // Sleep 1 sec
                Sleep(1000);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
        return null;
    }
    
    private void readStatus() throws IOException {
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(5000        /* milliseconds */);
            conn.setConnectTimeout(80000    /* milliseconds */);
            conn.connect();
            responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                in = conn.getInputStream();
                // Convert the InputStream into a string
                String contentAsString = readIt(in, 340);
                // close the inputstream
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            if (in != null) in.close();
        }
    }
    
    // Reads an InputStream and converts it to a String.
    public String readIt(InputStream stream, int len) throws IOException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[len];
        reader.read(buffer);
        return new String(buffer);
    }
    

    }