Android-如果语句被忽略和忽略

问题描述 投票:0回答:1

我正在从类似这样的函数中调用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")

没有积极的结果,那我在做什么错了?

php android json
1个回答
0
投票

您的response包含多余的新行\n,这就是if不起作用的原因。

© www.soinside.com 2019 - 2024. All rights reserved.