【问题标题】:Android Scanner Read Until a Delimiter (UseLimiter)Android 扫描仪读取直到分隔符 (UseLimiter)
【发布时间】:2016-08-19 18:45:58
【问题描述】:

我有一个发送大字符串的服务器。当我看到最后的分隔符时,我想停止阅读。

安卓:

 protected Void doInBackground(Void... arg0) {


    Socket socket = null;

    try {
        socket = new Socket(dstAddress, dstPort);

        Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));

        while (true) {


            //MASTER
            valores[0] = r.nextLine();
            valores[1] = r.nextLine();
            valores[2] = r.nextLine();
            valores[3] = r.nextLine();
            valores[4] = r.nextLine();
            valores[5] = r.nextLine();
            valores[6] = r.nextLine();
            valores[7] = r.nextLine();
            valores[8] = r.nextLine();
            valores[9] = r.nextLine();
            valores[10] = r.nextLine();
            valores[11] = r.nextLine();
            valores[12] = r.nextLine();

            //SLAVE
            valores[13] = r.nextLine();
            valores[14] = r.nextLine();
            valores[15] = r.nextLine();
            valores[16] = r.nextLine();
            valores[17] = r.nextLine();
            valores[18] = r.nextLine();
            valores[19] = r.nextLine();
            valores[20] = r.nextLine();
            valores[21] = r.nextLine();
            valores[22] = r.nextLine();
            valores[23] = r.nextLine();
            valores[24] = r.nextLine();
            valores[25] = r.nextLine();

            r.useDelimiter("\\zfish");
            while (r.hasNext()) {
                valores[26] = r.next();
            }

            publishProgress(valores[0], valores[1], valores[2], valores[3], valores[4], valores[5], valores[6], valores[7], valores[8], valores[9], valores[10], valores[11], valores[12],
                    valores[13], valores[14], valores[15], valores[16], valores[17], valores[18], valores[19], valores[20], valores[21], valores[22], valores[23], valores[24], valores[25], valores[26]);
        }

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return null;

}

服务器 C#

            string conetado, bateria, tensao, altitude, roll, pitch, yaw, velx, vely, velz, estado, atual, desejado;
            string conetado2, bateria2, tensao2, altitude2, roll2, pitch2, yaw2, velx2, vely2, velz2, estado2, atual2, desejado2;

            conetado = master.sconetado;
            bateria = master.sbateria;
            tensao = master.stensao;
            altitude = master.saltitude;
            roll = master.sroll;
            pitch = master.spitch;
            yaw = master.syaw;
            velx = master.svelx;
            vely = master.svely;
            velz = master.svelz;
            estado = master.sestado;
            atual = master.satual;
            desejado = master.sdesejado;


            conetado2 = slave.sconetado;
            bateria2 = slave.sbateria;
            tensao2 = slave.stensao;
            altitude2 = slave.saltitude;
            roll2 = slave.sroll;
            pitch2 = slave.spitch;
            yaw2 = slave.syaw;
            velx2 = slave.svelx;
            vely2 = slave.svely;
            velz2 = slave.svelz;
            estado2 = slave.sestado;
            atual2 = slave.satual;
            desejado2 = slave.sdesejado;

            string_master_frame = Convert.ToBase64String(sendBytes);

            data = conetado + "\n" + bateria + "\n" + tensao + "\n" + altitude + "\n" + roll + "\n" + pitch + "\n" + yaw + "\n" + velx + "\n" + vely + "\n" + velz + "\n" + estado + "\n" + atual + "\n" + desejado + "\n" +
                        conetado2 + "\n" + bateria2 + "\n" + tensao2 + "\n" + altitude2 + "\n" + roll2 + "\n" + pitch2 + "\n" + yaw2 + "\n" + velx2 + "\n" + vely2 + "\n" + velz2 + "\n" + estado2 + "\n" + atual2 + "\n" + desejado2 + "\n" + string_master_frame + "fish";

            send(data)

我的 Android 代码无法正常工作。它卡在 value = r.next();永远不要离开这条线。所以这意味着它不是在寻找鱼

【问题讨论】:

    标签: java c# android delimiter


    【解决方案1】:

    不要在循环中一遍又一遍地调用 r.useDelimiter()! 另外,如果设置为 true,您如何跳出循环?!

        Scanner r = new Scanner(new InputStreamReader(socket.getInputStream()));
        r.useDelimiter("\\s*fish\\s*"); // to find " fish " with space
    
        while (r.hasNext()) {
            value = r.next();
        }
        r.close(); // very important
    

    【讨论】:

    • 已编辑。为什么我不能使用while循环?这种方式总是读取和更新 TextViews 对吗?为什么每次阅读都需要关闭连接?
    • 您必须在某个地方跳出循环,这就是您所说的卡住的原因。否则它将无限期地运行!
    • 我有 while(true) 直到现在它运行良好。我坚持 while (r.hasNext()) { value = r.next(); }
    • 卡住是什么意思?
    • 您的代码不起作用。我需要在服务器上更改为“鱼”吗?包括空格?
    猜你喜欢
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多