android:将json数据发送到服务器

时间:2018-02-03 12:34:48

标签: php android mysql

我想将json数据发送到服务器。

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);

}

}

这是我的PHP代码:

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

$user = $postvars['un'];

$pass = $postvars['p'];


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

此echo语句显示最后一行数据,如(id:5 , title:val,price : val , quantity = val )。我想逐行访问。

或者请建议任何其他技术来安排json数据。

感谢。

1 个答案:

答案 0 :(得分:1)

您正在覆盖您的数据,并且只将最后一个原始数据传递给服务器。您必须使用JsonArray来存储所有数据

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());
相关问题