Android 没有来自jsonArray请求的响应
长话短说,我面临这种问题,我试图向我的服务器发送截击请求,服务器用json数组响应:Android 没有来自jsonArray请求的响应,android,json,android-volley,Android,Json,Android Volley,长话短说,我面临这种问题,我试图向我的服务器发送截击请求,服务器用json数组响应: { "images": [{ "product_serial_num": "1", "product_title": "Abbadon", "product_img": "http://1.2.3.4/android/uploads/1.jpg", "product_price": "750", "product_descr
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}
但无论我尝试了什么,我仍然会收到截击响应错误,我的截击请求:
requestQueue = Volley.newRequestQueue(getActivity());
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(MY_URL ,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonArrayRequest);
requestQueue=Volley.newRequestQueue(getActivity());
//JsonArrayRequest截击
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(我的URL,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
//parseData来解析json响应
解析数据(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//若发生错误,则表示已到达列表末尾
Toast.makeText(getActivity(),“没有更多可用项”,Toast.LENGTH_SHORT.show();
}
});
add(jsonArrayRequest);
奇怪的是,在截击字符串请求中,它确实起作用了,只有数组对我来说是一团糟
编辑:
我是如何使用提要获取阵列的:
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(URL_INDEX + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
private void getData(){
//通过调用getDataFromServer方法将该方法添加到队列
add(getDataFromServer(requestCount));
//递增请求计数器
requestCount++;
}
//请求从服务器获取json我们在这里传递一个整数
//此整数将用于指定请求的页码?page=requestcount
//此方法将返回将添加到请求队列的JsonArrayRequest
私有JsonArrayRequest getDataFromServer(int requestCount){
//JsonArrayRequest截击
JsonArrayRequest JsonArrayRequest=新的JsonArrayRequest(URL_INDEX+String.valueOf(requestCount),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
//调用parseData方法来解析json响应
解析数据(响应);
}
},
新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//若发生错误,则表示已到达列表末尾
Toast.makeText(getActivity(),“没有更多可用项”,Toast.LENGTH_SHORT.show();
}
});
//返回请求
返回jsonArrayRequest;
}
谢谢大家! 使用此代码,将JsonArrayRequest更改为JsonObjectRequest
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, MY_URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
对象响应
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}
使用此代码,将JsonArrayRequest更改为JsonObjectRequest
JsonObjectRequest jsonArrayRequest = new JsonObjectRequest(Request.Method.GET, MY_URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//parseData to parse the json response
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "No More Items Available", Toast.LENGTH_SHORT).show();
}
});
对象响应
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
{
"images": [{
"product_serial_num": "1",
"product_title": "Abbadon",
"product_img": "http://1.2.3.4/android/uploads/1.jpg",
"product_price": "750",
"product_description": "The destroyer"
}]
}
响应Json是JSONObject而不是JSONArray。使用凌空的
JsonObjectRequest
而不是JsonArrayRequest
object和string这两种方法都有效,只有数组不起作用,因为响应类型是JSONObjecttry@vinoth12594 ansewerChange url方法类型POST或GETResponse Json是JSONObject而不是JSONArray。使用凌空的JsonObjectRequest
而不是JsonArrayRequest
对象和字符串都能工作,只有数组不工作,因为响应类型是JSONObjecttry@vinoth12594 ansewerChange url方法类型POST或GETit,但为什么我不能使用数组请求?您的响应不是arraysec,我将显示不同的代码片段,使用arrayReq有什么区别?在编辑中添加,使用feedOne json请求您无法获得两种类型的响应。它正在工作,但为什么我不能使用arrayReq?您的响应不是arraysec对象,我将显示不同的片段,使用arrayReq有什么区别?在编辑中添加,使用feedOne json请求您无法获得两种类型的响应。