1

I have the following json response

{"multicast_id":8XXXD,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:14XX"}]}

I want to check if failure = 0 or not in java. Following is my Volley code.

StringRequest stringRequest = new StringRequest(Request.Method.POST, CONN_URL.send_single_push,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("title", title);
            params.put("message", message);

            if (!TextUtils.isEmpty(image))
                params.put("image", image);

            params.put("course", course);
            return params;
        }
    };

    MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);

I am new to android. Please help me solve this problem.

1
  • 1) the json isnt valid. 2) you get it by json.getInt("failure"); Commented Apr 9, 2018 at 19:52

1 Answer 1

5

You can do it by doing like this.

First you need to Convert String response to JSON response

StringRequest stringRequest = new StringRequest(Request.Method.POST, CONN_URL.send_single_push,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                JSONObject obj = new JSONObject(response);
                int failure = obj.optInt("failure");
                if (failure == 0){
                }else{  
                }
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<>();
        params.put("title", title);
        params.put("message", message);

        if (!TextUtils.isEmpty(image))
            params.put("image", image);

        params.put("course", course);
        return params;
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.