Android - 如果语句被忽略和绕过
Posted
技术标签:
【中文标题】Android - 如果语句被忽略和绕过【英文标题】:Android - If statement gets ignored and bypassed 【发布时间】:2020-05-20 02:02:55 【问题描述】:我正在从这样的函数调用 php 脚本:
public static String XSSignUp(String username, String password, String email, String signInWith)
// Paramenters
Map<String, Object> params = new LinkedHashMap<>();
params.put(USERS_USERNAME, username);
params.put(USERS_PASSWORD, password);
params.put(USERS_EMAIL, email);
params.put("signInWith", signInWith);
params.put(USERS_ios_DEVICE_TOKEN, IOS_DEVICE_TOKEN);
params.put(USERS_android_DEVICE_TOKEN, ANDROID_DEVICE_TOKEN);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, Object> param : params.entrySet())
if (postData.length() != 0) postData.append('&');
try postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
catch (UnsupportedEncodingException e) e.printStackTrace();
postData.append('=');
try postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
catch (UnsupportedEncodingException e) e.printStackTrace();
byte[] postDataBytes;
postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try
URL url;
url = new URL(TABLES_PATH + "m-signup.php?");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(20000);
conn.setReadTimeout(20000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
// Get response
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
InputStream responseStream = new BufferedInputStream(conn.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) stringBuilder.append(line).append("\n");
responseStreamReader.close();
String response = stringBuilder.toString();
responseStream.close();
conn.disconnect();
Log.i(TAG, "XSSignUp -> RESPONSE: " + response + "\n-----------------\n");
if (response.equals("e_101")) return E_101;
else if (response.equals("e_102")) return E_102;
else return response;
// error
else return "Something went wrong. Try again.";
catch (IOException e) e.printStackTrace(); return e.getMessage();
这就是我调用该函数的方式:
final String sup = XSSignUp(usernameTxt.getText().toString(), passwordTxt.getText().toString(), emailTxt.getText().toString(), "");
Log.i(TAG, "SUP: " + sup);
// errors
if (sup.matches("e_101"))
hideHUD();
simpleAlert(E_101, ctx);
else if (sup.matches("e_102"))
hideHUD();
simpleAlert(E_102, ctx);
else
Log.i(TAG, "YES, SIGN UP!");
所以,如果我运行我的应用程序并使用 johndoe 作为用户名填写注册表单,我的 PHP 脚本会返回一个响应字符串为“e_101
”(用户名已经存在),它会阻止将记录添加到我的数据库的脚本。我在 Logcat 中收到此消息:
I/log-: XSSignUp -> RESPONSE: e_101
I/log-: SUP: e_101
I/log-: YES, SIGN UP!
这是错误的,因为我不应该得到最后一行:I/log-: YES, SIGN UP!
。
这会损害我的应用程序,因为它不会触发警报对话框 (simpleAlert(E_101, ctx);
),而是继续并跳过该部分。
我真的不明白为什么 IF 语句不起作用,因为我也尝试过这样做:
final String sup = XSSignUp(usernameTxt.getText().toString(), passwordTxt.getText().toString(), emailTxt.getText().toString(), "");
sup = "e_101"; <-- FORCING THE sup STRING TO BE "e_101"!
// errors
if (sup.matches("e_101"))
hideHUD();
simpleAlert(E_101, ctx);
else if (sup.matches("e_102"))
hideHUD();
simpleAlert(E_102, ctx);
else
Log.i(TAG, "YES, SIGN UP!");
然后就可以了!但这对我来说没有任何意义,因为 sup
字符串与我的函数从 PHP 脚本返回的字符串相同,正如您从 Logcat 消息中看到的那样......
我也尝试过使用 equals():
sup.equals("e_101")
没有阳性结果,那我做错了什么?
【问题讨论】:
尝试使用sup.equalsIgnoreCase("e_101")
@Md.Asaduzzaman 我试过了,但我得到了相同的结果:(
检查我的答案
【参考方案1】:
您的response
包含额外的新行\n
,这就是if
不起作用的原因。
问题出在这里:
stringBuilder.append(line).append("\n");
尝试如下更改:
int i = 0;
while ((line = responseStreamReader.readLine()) != null)
if(i != 0)
stringBuilder.append("\n");
stringBuilder.append(line);
i++;
或者
....
stringBuilder.replace(stringBuilder.lastIndexOf("\n"), stringBuilder.length(),"");
String response = stringBuilder.toString();
除此之外,当您将 response
的 CASE 更改为内部 XSSignUp
并与外部的小写 CASE 进行比较时,您必须使用 equalsIgnoreCase
而不是 equals
之类的
sup.equalsIgnoreCase("e_101")
【讨论】:
非常感谢,换行符是问题所在,我无法弄清楚。以上是关于Android - 如果语句被忽略和绕过的主要内容,如果未能解决你的问题,请参考以下文章