【问题标题】:Check if JSON response contains certain value检查 JSON 响应是否包含特定值
【发布时间】:2018-02-13 11:30:02
【问题描述】:

所以我在 Android Studio 中为一个具有登录功能的网页构建了一个 Android 应用。我已经成功地将应用程序连接到网站数据库,我可以登录并从服务器获得响应。应用程序正在响应中查找错误值,但响应中不包含错误值。

这是我的应用程序中的代码:

public void onResponse(String response) {
            Log.d(TAG, "Login Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                // Check for error node in json
                if (!error) {
                    // user successfully logged in
                    // Create login session
                    session.setLogin(true);


                    // Launch main activity
                    Intent intent = new Intent(LoginActivity.this,
                            MainActivity.class);
                    startActivity(intent);
                    finish();

这是登录成功时服务器给出的响应:

Login Response: {"howislife":1,"rekeningnummer":"212220","soortuser":"student","datum":"05 Sep 2017"}

所以当登录成功时会显示​​“howislife:1”,当密码或用户名不正确时会显示“howislife:2”或“howislife:3”。因此,我的 Android 应用程序中的代码需要检查“howislife”的值是什么。我怎样才能做到这一点?提前致谢!

【问题讨论】:

标签: java android json server response


【解决方案1】:

试试这个代码,

int howislife = (!jsonObjects.isNull("howislife")) ?
                                        jsonObjects.optInt("howislife") : 0;
            if (howislife == 1) {
                //login sucess
            } else {
               //login failed
            }

【讨论】:

    【解决方案2】:

    我假设你的 api 总是使用这些参数给出响应,如果响应是 emptynull 则在转换为 JSONObject 之前检查它,那么你可以试试下面的代码,它会工作的。

    JSONObject jObj = new JSONObject(response);
                if(jObj.getInt("howislife")==1){
                      System.out.println("Login success");
                }else{
                      System.out.println("Login fail");
                }
    

    【讨论】:

      【解决方案3】:

      Android 中有一个函数“has()”来检查 JSONObject 的键。

      布尔具有(字符串名称) 如果此对象具有名称映射,则返回 true。映射可能为 NULL。

      更多细节:

      https://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

      【讨论】:

      • 作为替代方案 - JSONObject#optInt(String name) 将返回 Optional,然后可以使用该选项构建逻辑。
      【解决方案4】:

      我已经用这个代码解决了,谢谢!

      public void onResponse(String response) {
                  Log.d(TAG, "Login Response: " + response.toString());
                  hideDialog();
      
                  try {
                      JSONObject jObj = new JSONObject(response);
                      int login1 = jObj.getInt("howislife");
                      System.out.println(login1);
                      //Check for error node in json
                      if (login1 == 1) {
                          // user successfully logged in
                          // Create login session
                          session.setLogin(true);
      
      
                          // Launch main activity
                          Intent intent = new Intent(LoginActivity.this,
                                  MainActivity.class);
                          startActivity(intent);
                          finish();
                      } else if(login1 == 2) {
                          Toast.makeText(getApplicationContext(), "Wachtwoord verkeerd", Toast.LENGTH_LONG).show();
                      } else if(login1 == 3) {
                          Toast.makeText(getApplicationContext(), "Gebruikersnaam of/en wachtwoord verkeerd", Toast.LENGTH_LONG).show();
                      } else {
                          Toast.makeText(getApplicationContext(), "Er is iets fout gegaan, probeer opnieuw.", Toast.LENGTH_LONG).show();
                      }
                  } catch (JSONException e) {
                      // JSON error
                      e.printStackTrace();
      
                  }
      
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2016-02-04
        • 1970-01-01
        • 2012-10-17
        • 2020-05-24
        相关资源
        最近更新 更多