0

I want to send json data to the server.

final JSONObject root = new JSONObject();
    Integer i = 0;

    for(Cart_Product cart_product : cartArraylist)

    {

        final String title = cart_product.getTitle();
        final Double price = cart_product.getPrice();
        final Integer quantity= cart_product.getCountvalue();

        try {
            root.put("id",i);
            root.put("title",title);
             root.put("price",price);
             root.put("quantity",q);
             i++;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
      Map<String, String> postParam= new HashMap<String, String>();
    postParam.put("un", root.toString());


    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            Constants.URL_ORDER, new JSONObject(postParam),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.toString());

                        Toast.makeText(getApplicationContext(),
                                jsonObject.getString("message"),
                                Toast.LENGTH_LONG).show();

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {

        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            return headers;
        }



    };

    MySingleton.getInstance(this).addToRequestQueue(jsonObjReq);

}

}

And here is my php code is :

$body = file_get_contents('php://input');
$postvars = json_decode($body,true);

$user = $postvars['un'];

$pass = $postvars['p'];


$response['message']=$user; 
echo json_encode($response);

This echo statement is showing the last row of data like (id:5 , title:val,price : val , quantity = val ) . I want to access one by one each row.

OR please suggest any other technique to arrange json data.

Thanks.

1 Answer 1

1

You are overwriting your data,and passing only last raw to server.You have to take a JsonArray for store all the data

JSONArray arrayRoot=new JSONArray();

 Integer i = 0;
for(Cart_Product cart_product : cartArraylist)

    {

        final JSONObject root = new JSONObject();
        final String title = cart_product.getTitle();
        final Double price = cart_product.getPrice();
        final Integer quantity= cart_product.getCountvalue();

        try {
            root.put("id",i);
            root.put("title",title);
             root.put("price",price);
             root.put("quantity",q);
             arrayRoot.put(i,root );//add JSONObject to array
             i++;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
  postParam.put("un", arrayRoot.toString());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.