WebView 的 postUrl 方法仅适用于显式字符串

WebView's postUrl method only works with explicit string

我已经可以看出这是一个愚蠢的问题,但我正在尝试使用 postUrl() 登录网页,当我明确地写入 post 数据时,如下所示:

String postData = "username=johndoe&password=mypassword";
myWebview.postUrl("https://moodle.domain.com/login/index.php", postData.getBytes());

它有效,但如果我使用变量构造 post数据:

String postData = "username="+user+"&password="+pass;
myWebview.postUrl("https://moodle.domain.com/login/index.php", postData.getBytes());

没用。凭据错误。我已验证这两个字符串具有完全相同的值,并且我尝试使用 UTF-8。

我就是不明白,这没有任何意义。

试试下面的方法

String postData = "username=" + URLEncoder.encode(my_username, "UTF-8") + "&password=" + URLEncoder.encode(my_password, "UTF-8");
 webview.postUrl("https://moodle.domain.com/login/index.php",postData.getBytes());

看起来像,'user' 或 'pass' 有不可见的符号。您需要:

1) 使用 equals:

检查字符串
 String postData1 = "username=johndoe&password=mypassword";
 String postData2 = "username="+user+"&password="+pass;
 System.out.println(postData1.equals(postData2));

2) 尝试寻找不可见的符号,例如:

 String postData1 = "username=johndoe&password=mypassword";
 String postData2 = "username="+user+"&password="+pass;
 char[] arr1 = postData1.toCharArray();
 char[] arr2 = postData1.toCharArray();

 for (int i=0; i< Math.max(arr1.length(), arr2.length(); i++ ) {
     if(arr1[i] != arr2[i]) {
         System.out.println("Different i " + i);
     } 
 }