通过截取JSONObjectRequestRespone循环,并将参数发送到php
我正在使用jsonobjectrequest将我的参数发送到一个php页面,该页面将返回所需的结果, 到目前为止,我在android代码中所做的是获得我想要传递到jsonobject的值,如下所示通过截取JSONObjectRequestRespone循环,并将参数发送到php,php,android,android-volley,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(Me
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.
而且响应长度是5,但我无法读取其中的数据!
用这个
response.getJSONArray("item_id").getString(i)
我总是得到一个空值
谢谢试试这个:
示例JSON字符串:
{
"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();
}
}
public void onResponse(JSONObject-response){
Log.d(“Response:,Response.toString());
试一试{
JSONArray jsArr=response.getJSONArray(“产品”);
如果(jsArr.length()>0){
对于(int i=0;i
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();
}
}