【问题标题】:BufferedReader not reading all linesBufferedReader 没有读取所有行
【发布时间】:2013-03-23 20:29:04
【问题描述】:

我正在创建一个使用蓝牙连接与 PC 通信的 Android 应用程序。我正在尝试将 ArrayList 中的项目从 PC 发送到 Android。

【问题讨论】:

    标签: android bufferedreader outputstream ioexception java-io


    【解决方案1】:

    尝试刷新输出流并关闭输出流。这应该将缓冲区中的剩余数据刷新到 Android 设备。您还需要为每个输出添加一个新的换行符(如果 ArrayList 的值中不存在一个),即在您的 PC 代码上执行以下操作:

                PC Code:
    
    
                list = FilterFile.aList;
                for(int z = 0; z < list.size(); z++) {
                    int a = list.size();
                    System.out.println("in for\n"+list.size());
                    String str = list.get(z);
                    System.out.println(str);
                    out.write((str+"\n").getBytes());
                    out.flush();
                }  
                out.close();     
    
    
                Android (Client) side code:
    
    
                public class ConnectedThread extends Thread {
            private BluetoothSocket mmSocket;
            private InputStream mmInStream = null;
            public OutputStream mmOutStream = null;
            private static final String TAG = "ConnectedThread";
            private static final boolean D = true; 
            int i;
            char a;
            String line;
            String y = "";
            //DataInputStream din=null;
    
            public ConnectedThread(BluetoothSocket socket) {
                mmSocket = socket;
                InputStream tmpIn = null;
                OutputStream tmpOut = null;
                try {
                    tmpIn = socket.getInputStream();
                    tmpOut = socket.getOutputStream();
                } catch (IOException e) { }
    
                mmInStream = tmpIn;
                mmOutStream = tmpOut;
                //din = new DataInputStream(mmInStream);
                if (D) Log.e(TAG, "-- in connected() --");
            }
    
            public void run() {
                if (D) Log.e(TAG, "-- ConnectedThread --");                 
            }
    
            public void write(byte[] bytes) {
                try {
                    mmOutStream.write(bytes);
                } catch (IOException e) { }
            }
    
            public String readList() {
    
                            String data = "";
                try {
                    byte buff[] = new byte[1024];
                    int count = -1;
    
                    while((count = mmInStream.read(buff, 0, 1024)) != -1){
                        data += new String(buff,0,count);
                    }
                    String lines[] = data.split("\n");
    
                    for(int i = 0; i < lines.length; i++){
    
                        //this will print your individual lines...
                        System.out.println(lines[i]);
                    }
    
    
                } catch (Exception e) {
                    ///print your error message here
                }   
                finally{
                if(in != null){
                    try {
                    mmInStream.close();
                    } catch (Exception e) {
                    e.printStackTrace();
                    }
                }
               }
               return data;
                }
    
            public void cancel() {
                try {
                    mmSocket.close();
                } catch (IOException e) { }
            }
    

    }

    【讨论】:

    • 当我尝试这种方法时,我得到了帖子中显示的错误
    • 您发布的日志中没有错误。请查看并尝试找到显示异常的位置。还有你是怎么知道有错误的?
    • 这不是一个例外,但它似乎陷入了 while 循环并最终停止执行任何操作。我已经用修改后的代码和结果更新了帖子
    • 关于如何解决此问题的任何想法?
    • 好的,向前迈出一步,您现在收到的是多行而不是一行..您是否接收到从 PC 发送的所有行?我已经在我的帖子中更新了 PC 代码,添加这一行 out.write("\n".getBytes());让我知道会发生什么
    【解决方案2】:

    尝试只调用一次write 方法(并在调用后刷新/关闭缓冲区):

    String str = "";
    for(int z = 0; z < list.size(); z++) {
        str += list.get(z);
    }
    
    System.out.println(str);
    out.write(str.getBytes());
    out.flush();
    out.close();
    

    【讨论】:

    • 这可能很愚蠢,但你试过吗? System.out.println(connectedThread.read().toString());
    • 我试过了,还是有同样的问题。我认为 read() 方法存在问题,因为当我打印“行”字符串时,它会打印 ArrayList 中的每个项目。然后它似乎卡住了,Android 应用程序冻结了。
    • 我已经编辑了我的帖子,因此您可以看到显示的输出。
    • 读取您的输出似乎 readLine() 方法可以正常工作,尝试使用普通字符串而不是 stringBuilder,只需在方法开头初始化字符串,并在 for 循环内使用 yourString += ...
    • 另外,您可以将 read() 方法的返回值保存在 StringBuffer 变量中,这样您就可以调试并查看它是否为 null 和/或它实际包含的内容。 StringBuffer sb = connectedThread.read();字符串 s = sb.toString(); System.out.println(s);
    猜你喜欢
    • 2015-05-12
    • 2018-06-16
    • 2017-10-20
    • 2017-11-18
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多