title: Android Socket BufferedReader readLine() not working when reading from the InputStream
body: <p>I am trying to loop code inside a thread in order to check for new data form the server periodically. I am using <code>InputStream.available()</code> in order to check if there are any bytes to be read. If so, I open a new Thread and try to read the data using <code>BufferedReader</code> and then log it to the console.</p>
  <p><strong>In onCreate:</strong></p>
  <pre><code>HandlerThread ht = new HandlerThread(&quot;CheckForData&quot;);
                  ht.start();
                  Looper checkForDataLooper = ht.getLooper();
                  checkForDataHandler = new Handler(checkForDataLooper);
                  checkForDataHandler.post(checkForData);
  </code></pre>
  <p><strong>The two runnables:</strong></p>
  <pre><code>Runnable checkForData = new Runnable() {
          @Override
          public void run() {
              try {
                  Log.d(&quot;Count&quot;, &quot;run: &quot; + inputStream.available());
                  if (inputStream.available() &gt; 0) {
                      checkForDataHandler.removeCallbacks(checkForData);
                      new Thread(parseData).start();
                  }
              }
              catch (Exception e) {
                  Log.e(&quot;Exception&quot;, &quot;run: &quot; + e);
              }
              checkForDataHandler.postDelayed(checkForData, 3000);
          }
      };
  </code></pre>
  <pre><code>Runnable parseData = new Runnable() {
          @Override
          public void run() {
              String line = null;
              StringBuilder data = new StringBuilder();
              try {
                  while ((line = in.readLine()) != null) {
                      data.append(line);
                  }
                  Log.d(&quot;PARSED DATA&quot;, &quot;run: &quot; + data);
              }
              catch (Exception e) {
                  e.printStackTrace();
              }
          }
      };
  </code></pre>
  <p><strong>The console:</strong></p>
  <pre><code>D/Count: run: 0
  D/AwareBitmapCacher: handleInit switch not opened pid=26256
  D/Count: run: 0
  D/Count: run: 0
  D/Count: run: 4
  D/Count: run: 0
  D/Count: run: 0
  D/Count: run: 0
  D/Count: run: 0
  W/System.err: java.net.SocketException: Connection reset
          at java.net.SocketInputStream.read(SocketInputStream.java:215)
          at java.net.SocketInputStream.read(SocketInputStream.java:144)
  W/System.err:     at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:291)
          at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:355)
          at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:181)
          at java.io.InputStreamReader.read(InputStreamReader.java:184)
          at java.io.BufferedReader.fill(BufferedReader.java:172)
          at java.io.BufferedReader.readLine(BufferedReader.java:335)
          at java.io.BufferedReader.readLine(BufferedReader.java:400)
  W/System.err:     at com.bitline.httpcomtest.MainActivity$3.run(MainActivity.java:121)
          at java.lang.Thread.run(Thread.java:929)
  </code></pre>
  <p>The 5th line in the console represents the moment when I send some data from the server. I can see that the number of bytes in the InputStream modifies, then the BufferedReader consumes them, but the data isn't logged to the console.
  The exception throws when I force close the server.</p>
  <p>PS. I am new to threading and networking so I apologize if this is some rookie mistake I am making.</p>
  <p>Thank you.</p>