我正在构建Android应用,我需要使用localhost
中的一些数据填充自定义列表视图。我试图使用volley JsonArrayRequest
以JSONObject数组的格式获取数据,但我得到的只是org.json.JSONException: End of input at character 0 of
。这是我的json数组请求:
final String URL = "http://10.0.3.2/androidapi/index.php";
JsonArrayRequest productsReq = new JsonArrayRequest(Method.POST, URL, new Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("productTAG", response.toString());
for(int i = 0; i < response.length(); i++)
{
try
{
JSONObject productObj = response.getJSONObject(i);
String title = productObj.getString("title");
String description = productObj.getString("description");
String category = productObj.getString("category");
String subCategory = productObj.getString("subCategory");
String size = productObj.getString("size");
String price = productObj.getString("price");
String thumbnailUrl = productObj.getString("image_one");
Product product = new Product(title, thumbnailUrl, description, category, subCategory, size, price);
products.add(product);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("products_error", error.getMessage().toString());
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("req", "products");
params.put("category", category);
params.put("subCategory", subCategory);
return params;
}
};
VolleyCore.getInstance(getActivity()).addToRequestQueue(productsReq);
我使用的方法是POST
,在getParams
下我正在添加名为req
的标记,其值为products
。我正在index.php
中检查此标记。这是我查询表格的函数:
public function getItems($category, $subCategory)
{
$query = mysql_query("SELECT * FROM items WHERE category = '$category' AND sub_category = '$subCategory'") or die(mysql_error());
$objects = array();
if($query)
{
while($objects = mysql_fetch_assoc($query))
{
$final[] = $objects;
}
}
return $final;
}
我在index.php中检查请求:
if(isset($_POST['req']) && $_POST['req'] == 'products')
{
$category = $_POST['category'];
$subCategory = $_POST['subCategory'];
$obj = $func->getItems($category, $subCategory);
echo json_encode($obj);
}
$func
是类的实例化,我保留所有使用数据库的方法(包括getItems()
)。如果我使用volley StringRequest
我会收到回复,但是为什么我在使用JsonArrayRequest
时没有收到回复?我做错了什么?
编辑
回复示例
06-28 15:05:58.750: D/productTAG(3853):
[
{
"_id":"2",
"user_email":"unemail",
"title":"fsdfsd",
"description":"dffdhfg",
"price":"12",
"quantity":"12",
"size":"L",
"category":"Men",
"sub_category":"Shoes",
"image_one":"base 64 code…",
"image_two":"base 64 code..",
"image_three":"base 64 code"
},
{
"_id":"3",
"user_email":"unemail",
"title":"fsdfsd",
"description":"dffdhfg",
"price":"12",
"quantity":"12",
"size":"L",
"category":"Men",
"sub_category":"Shoes" ,
"image_one":"base 64 code",
"image_two":"base 64 code",
"image_three":"base 64 code "
}
]
使用StringRequest
。
答案 0 :(得分:1)
@Override
public void onResponse(String response)
try {
JSONArray myArray = new JSONArray(response);
for(int i = 0; i < myArray.length(); i++)
{
JSONObject jObj = myArray.getJSONObject(i);
String category = jObj.getString("category");
Log.d("category", category);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
我无法弄清楚如何使用JsonArrayRequest
,所以我使用了我的问题中提到的StringRequest
并解析了这样的回复:
@Override
public void onResponse(String response) {
// Log.d("productTAG", response);
try {
JSONArray myArray = new JSONArray(response);
for(int i = 0; i < myArray.length(); i++)
{
JSONObject jObj = myArray.getJSONObject(i);
String category = jObj.getString("category");
Log.d("category", category);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这对我有用,也感谢@Peter P的帮助!