簡體   English   中英

如果對字符串的聲明不起作用

[英]If statement on string wont work

您好,我正在針對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

使用Arrays.toString(str.getBytes())轉儲字符串的字節內容。

我懷疑字符串的末尾會有很多空字節,因為outputOutStream沒有完全填充。 您需要的構造函數是String(byte [] bytes,int offset,int length) 以0作為偏移量並以bytesRead作為長度來調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM