简体   繁体   English

如果对字符串的声明不起作用

[英]If statement on string wont work

Hello so im working on an app for windows(C#) and android(Java) that the two communicate through TCP and i have got them both sending each other strings but when i want to see if the received string is equal to "abc", it wont work for some reason with the java side(I think). 您好,我正在针对Windows(C#)和android(Java)的应用程序进行工作,以使二者通过TCP进行通信,我让它们都互相发送字符串,但是当我想查看接收到的字符串是否等于“ abc”时,它不会与Java端出于某种原因工作(我认为)。 This is a bit of the java code for the android side 这是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();
}

But for some reason it wont have the AlertDialog pop up when I make the Windows(C#) send the string "abc" through. 但是由于某种原因,当我使Windows(C#)通过它发送字符串“ abc”时,它不会弹出AlertDialog

Dump the byte content of the string using Arrays.toString(str.getBytes()) . 使用Arrays.toString(str.getBytes())转储字符串的字节内容。

I suspect the string will have lots of null bytes at the end because outputOutStream is not completely filled. 我怀疑字符串的末尾会有很多空字节,因为outputOutStream没有完全填充。 The constructor you need is String(byte[] bytes, int offset, int length) . 您需要的构造函数是String(byte [] bytes,int offset,int length) Invoke it with 0 as the offset and bytesRead as the length. 以0作为偏移量并以bytesRead作为长度来调用它。

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

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