循环通过Volley JsonObjectRequest响应,并将params发送到php

时间:2014-12-26 16:19:57

标签: php android android-volley

我正在使用jsonobjectrequest将我的参数发送到php页面,该页面将返回所需的结果, 到目前为止,我在android代码中所做的是获取我想传递给jsonobject的值,如下所示

JSONObject Cat_id_Object = new JSONObject();
        Cat_id_Object.put("cat_id", "2");

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                url, Cat_id_Object,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) 
                    {
                        Log.d(TAG, response.toString());
                        pDialog.hide();
                        Toast.makeText(getActivity(), "data received! "+response.length(), Toast.LENGTH_SHORT).show();
                        for (int i = 0; i < response.length(); i++) 
                        {
                            try {
                                ItemsProperty Item = new ItemsProperty();
                                Item.setitem_id(response.getJSONArray("item_id").getString(i));
                                Item.setitem_name(response.getJSONArray("item_name").getString(i));
                                Item.setitem_price(response.getJSONArray("item_price").getString(i));
                                Item.setitem_img(response.getJSONArray("item_img").getString(i));

                                test = (response.getJSONArray("item_name").getString(2).toString());

                                ItemsList.add(Item);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            adapter.notifyDataSetChanged();
                        }
                        Toast.makeText(getActivity(), "data received! " + test, Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        pDialog.hide();
                        Toast.makeText(getActivity(), "data NOT received!", Toast.LENGTH_SHORT).show();
                    }
                });
 the response.length() always returns 5, which is the true result if i was sending a zero parameter to the php page.

这是php页面:

$cid = $_POST['cat_id'];
$sql = "SELECT item_id,item_name,item_price,item_img FROM items where cat_id = '".$cid."' ";

并且响应长度为5但我无法读取其中的数据! 使用这个

response.getJSONArray("item_id").getString(i)

我总是得到一个空值

由于

1 个答案:

答案 0 :(得分:0)

试试这个:

示例JSON STRING:

 {
  "products": [
    {
      "item_id": "1",
      "item_name": "Item1",
      "item_price": 20
    },
    {
      "item_id": "1",
      "item_name": "Item2",
      "item_price": 30
    }
 ]
 }

您的回答应该是:

public void onResponse(JSONObject response) {
                        Log.d("Response: ", response.toString());
                        try {
                            JSONArray jsArr = response.getJSONArray("products");
                            if (jsArr.length() > 0) {

                                for (int i = 0; i < jsArr.length(); i++) {
                                    ItemsProperty item = new ItemsProperty ();
                                    item.item_id= jsArr.getJSONObject(i).getString("item_id");
                                    item.item_name= jsArr.getJSONObject(i).getString("item_name");
                                    item.item_price= jsArr.getJSONObject(i).getString("item_price");
                                    ItemsList.add(i, item);
                                }
                                mAdapter.notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            Log.e("REGISTER ERROR", "JSON Parsing error: " + e.getMessage());
                            e.printStackTrace();
                        }
                    }