如果字符串上的语句不起作用

时间:2013-05-26 01:18:18

标签: java android string if-statement

您好,我正在开发一个应用程序的Windows(C#)和android(Java),这两个通过TCP进行通信,我让他们发送彼此字符串,但当我想看看收到的字符串是否等于“ abc“,它不会出于某种原因与java方面(我认为)。这是android方面的一些java代码

int bytesRead;
byte[] outputOutStream = new byte[1024];
bytesRead = ins.read(outputOutStream, 0, outputOutStream.length);

String received;
received= Integer.toString(bytesRead);

String str = new String(outputOutStream);

txtView1.setText(str);

String code = "abc";
if (str.equals(code)) {
    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    alertDialog.setTitle("You Said...");
    alertDialog.setMessage(str);
    alertDialog.show();
}

但由于某种原因,当我使Windows(C#)发送字符串“abc”时,它不会弹出AlertDialog

1 个答案:

答案 0 :(得分:1)

使用Arrays.toString(str.getBytes())转储字符串的字节内容。

我怀疑字符串最后会有很多空字节,因为outputOutStream没有完全填满。您需要的构造函数是String(byte[] bytes, int offset, int length)。调用它作为偏移量为0,长度为bytesRead