Volley JsonObjectRequest response

前端 未结 3 1515
情书的邮戳
情书的邮戳 2021-01-25 14:04

I\'m retrieving data from my server through JSON using Volley JSONObjectRequest to achieve this. After getting the JSON response, I want to save it int

相关标签:
3条回答
  • 2021-01-25 14:42

    You should declare status as int instead of String.

    private int status=0;
    

    You can try this way

                    status = response.getInt("status");
                    description = response.getString("description");
                    if (status == 1) { 
                     checkingStatus(status);
                    } else {
                     checkingStatus(status);
                    }
    

    Then

    public void checkingStatus(int status){
            if(status == 1){
                // do your code
            }else{
                // do your code
            }
        }
    
    0 讨论(0)
  • 2021-01-25 15:03

    I have updated my answer as below,

        private int status;
        private String description;
    
        private void callRequest() {
    
            // Initialize a new RequestQueue instance
            RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    
            // Initialize a new JsonObjectRequest instance
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                    Request.Method.GET,
                    "URL",
                    null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                status = response.getInt("status");// ** STATUS IS 1 HERE **
                                description = response.getString("description");
    
                                checkStatus();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                        }
                    }
            );
    
            // Add JsonObjectRequest to the RequestQueue
            requestQueue.add(jsonObjectRequest);
        }
    
        private void checkStatus(){
            if(status == 1){
                // do what do you need to do id status == 1
            }else{
                // do what do you need to do id status == 0
            }
        }
    

    Try this and let me know the updates Thanks!

    0 讨论(0)
  • 2021-01-25 15:05

    The volley request is an asynchronous call. It doesn't block the code till the response is received. So what is happening is your check for if(status==1) is happening before the volley response is received. Hence it is 0.

    What you should do:

    1) Make your function return type void.

    2) In onResponse, put the code

    if (status == 1) { // ** STATUS IS 0 HERE **
            statusBoolean = true;
        } else {
            statusBoolean = false;
    }
    
    0 讨论(0)
提交回复
热议问题