Volley : how can I parse data String with integer by HashMap Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Volley : how can I parse data String with integer by HashMap without wasting too much if your time.

The question is published on by Tutorial Guruji team.

how can I parse data String with integer by HashMap I have String and integer data of json and the problem in HashMap he accept just String or integer value this Is structure of Json

   {
   "status":true,
   "message":"Get Products Successfully",
   "products":[
      {
         "p_id":48,
         "p_name":"Ford Red",
         "p_descrption":"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr",
         "p_price":15000,
         "p_offer_price":12000,
         "p_image":"myLinkimage"
      }
     ]
   }

this is my java code

 final ProgressDialog pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage("Loading...");
    pDialog.show();

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "myLink", null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {


                }
            }, 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("p_name","mac");
            params.put("p_descrption","mac mac 2018");
            params.put("p_price","10000");
            params.put("p_offer_price","8000");
            params.put("p_image","https://d.top4top.net/p_105212t9r1.jpg");
            return params;
        }
    };


    AppController.getInstance().addToRequestQueue(jsonObjectRequest);

and

params.put("p_price","10000");
params.put("p_offer_price","8000");

in my json structure it is a integer , how can I parse this data like a integer value ?

Answer

You don’t need to parse anything, rather use JsonObjectRequest as intended.

Which is to move the map from getParams method (and possibly remove the method entirely) and then create your payload ahead of the Volley call

JSONObject payload = new JSONObject();
JSONArray products = new JSONArray();

JSONObject product = new JSONObject();
product.putInt("price", 100);
products.add(product);

payload.putArray("products", products);

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "myLink", payload,
        new Response.Listener<JSONObject>() {

And if you want to parse the response, you can directly call response.getInt , for example

If you want, you can combine Volley with Gson, as pointed out in the Android documentation, which makes this implementation cleaner, or Retrofit w/ Gson would work similarly

We are here to answer your question about Volley : how can I parse data String with integer by HashMap - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji