【发布时间】: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 我试过了,但我得到了相同的结果:(
-
检查我的答案