How to display/request JSON Object using Android Volley?

前端 未结 3 1536
轮回少年
轮回少年 2021-01-28 18:36

I\'m having a problem in Android Studio on how to request JSON Object.

My Logcat can only print String onResponse but not JSONObject value. I\'m having a problem in lin

相关标签:
3条回答
  • 2021-01-28 19:12

    Your requesting a JsonObject? You seem to have a grasp on StringRequest, so ill keep it simple. Change your StringRequest to a JsonObjectRequest.

    0 讨论(0)
  • 2021-01-28 19:18

    Problem :

    As you can see in your logcat the response that you get from the server is success_access{"access":"PA001","password":"123","fullname":"ARCADE","branch":"HQ","section":"HQ"}

    First, this is not a valid JSON. Also, that is the value of the response variable. Now, your response.equals("success_access") will never return true, and hence the code control will never enter the if block.

    Solution :

    Ideally, there should be no extra string like success_access, insted your response should be a JSON object ALWAYS containing one specified field (the result feild for example). To show success, your JSON will look something like :

    {
        result : "success_access",
        /* other parameters down here */
    } 
    

    And in case of failure :

    {
        result : "access_failed"
    } 
    

    And then your code should convert the respose string into a JSON (your server code should make sure that it returns a valid JSON always) check for the value of the result field and proceed further.

    Quick Fix :

    Change your code to :

      ....
      Log.e(TAG, "Response is = " + response);
      String result = response.replace("{.*}","");
      String jsonString = response.replace(result, "");
    
      if(result.equals("success_access")){
        try {
                JSONObject jsonObject = new JSONObject(jsonString);
        ....
    
    0 讨论(0)
  • 2021-01-28 19:24

    Try this.

    In the AccessActivity

     Intent intent = new Intent(AccessActivity.this, NextActivity.class);
     intent.putExtra("access",   accessdb);
     intent.putExtra("password", passworddb);
     intent.putExtra("fullname", fullnamedb);
     intent.putExtra("branch",   branchdb);
     intent.putExtra("section",  sectiondb);
     AccessActivity.this.startActivity(intent);
    

    change

    LoginActivity.this.startActivity(intent);
    

    to

    AccessActivity.this.startActivity(intent);
    

    In the NextActivity

    Intent intent = getIntent();
    String access = intent.getStringExtra("access");
    String password = intent.getStringExtra("password");
    String fullname = intent.getStringExtra("fullname");
    String branch = intent.getStringExtra("branch");
    String section = intent.getStringExtra("section");
    

    Edited

    change

    if(response.equals("success_access")){
    }
    

    to

    if(response.contains("success_access")){
    }
    

    And according to your code. You need to make sure that your current Activity is com.apps.test.AccessActivity or com.apps.test.AccessActivity.

    Edited

    change to

    if (response.contains("success_access")) {
            String res = response.substring(response.indexOf("{"));
            try {
                JSONObject jsonObject = new JSONObject(res);
                final String accessdb = jsonObject.getString("access");
                final String passworddb = jsonObject.getString("password");
                final String fullnamedb = jsonObject.getString("fullname");
                final String branchdb = jsonObject.getString("branch");
                final String sectiondb = jsonObject.getString("branch");
                Log.e("Tag", accessdb);
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
     }
    
    0 讨论(0)
提交回复
热议问题