我从这样的函数调用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")
没有积极的结果,那我做错了什么?
您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
,以上肢内侧XSSignUp
与下壳外面比较,你必须使用equalsIgnoreCase
,而不是equals
像
sup.equalsIgnoreCase("e_101")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句