Skip to content
Advertisement

Android Socket BufferedReader readLine() not working when reading from the InputStream

I am trying to loop code inside a thread in order to check for new data form the server periodically. I am using InputStream.available() 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 BufferedReader and then log it to the console.

In onCreate:

HandlerThread ht = new HandlerThread("CheckForData");
                ht.start();
                Looper checkForDataLooper = ht.getLooper();
                checkForDataHandler = new Handler(checkForDataLooper);
                checkForDataHandler.post(checkForData);

The two runnables:

Runnable checkForData = new Runnable() {
        @Override
        public void run() {
            try {
                Log.d("Count", "run: " + inputStream.available());
                if (inputStream.available() > 0) {
                    checkForDataHandler.removeCallbacks(checkForData);
                    new Thread(parseData).start();
                }
            }
            catch (Exception e) {
                Log.e("Exception", "run: " + e);
            }
            checkForDataHandler.postDelayed(checkForData, 3000);
        }
    };
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("PARSED DATA", "run: " + data);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

The console:

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)

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.

PS. I am new to threading and networking so I apologize if this is some rookie mistake I am making.

Thank you.

Advertisement

Answer

I have found the answer: The server wasn’t sending n or r at the end of the data and BufferedReader.readLine() looks for one of these characters. As a result, the InputStream was being read endlessly.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement