简体   繁体   English

从 InputStream 读取时,Android Socket BufferedReader readLine() 不起作用

[英]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.我正在使用InputStream.available()来检查是否有任何要读取的字节。 If so, I open a new Thread and try to read the data using BufferedReader and then log it to the console.如果是这样,我打开一个新线程并尝试使用BufferedReader读取数据,然后将其记录到控制台。

In onCreate:在 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.控制台中的第 5 行代表我从服务器发送一些数据的时刻。 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.我可以看到 InputStream 中的字节数修改,然后 BufferedReader 使用它们,但数据没有记录到控制台。 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.谢谢你。

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.我找到了答案:服务器没有在数据末尾发送\\n\\r并且BufferedReader.readLine()查找这些字符之一。 As a result, the InputStream was being read endlessly.结果, InputStream被无休止地读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM