title: java.io.IOException: closed error in a non-stop loop inside an asynctask
body: <p>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).</p>
  
  <p>My problem is that when I execute the app, I have an java.io.IOException: closed exception the second time I do :</p>
  
  <pre><code>  reader.read(buffer);  // HERE I HAVE AN IOException closed
  </code></pre>
  
  <p>(first loop is ok, then I have error each loop)</p>
  
  <p>Thanks if someone can help me. I undesrtand the reason of the error, but I cannot find a solution...</p>
  
  <p>Here is my code :</p>
  
  <pre><code>class StatusnAsync extends AsyncTask&lt;Void, Void, Void&gt; {
      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);
      }
  
  }
  </code></pre>
  
  <p>Thank you.</p>