2016-07-26 71 views
0

好长话短说我面对这样的问题,我试着去发送凌空要求到我的服务器,它使用JSON阵列响应无响应:从jsonArray要求

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

奇怪的事情,与凌空串请求它的工作,只有数组乱七八糟的东西给我。

编辑:

怎么过这种方式我做饲料用得到我的数组:

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

谢谢!

+0

响应Json是JSONObject而不是JSONArray。凌空而不是'JsonArrayRequest' –

+0

对象和字符串都工作的 使用'JsonObjectRequest',只有阵列犯规 – 2Stoned

+0

由于响应类型是JSONObject的 –

回答

2

使用此代码,更改JsonArrayRequestJsonObjectRequest

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" 
}] 
} 
+0

它做的工作,但为什么不能使用数组req? – 2Stoned

+0

你的回应是对象不是数组 – vinoth12594

+0

秒虐待显示你不同的片段,与arrayReq有什么区别? – 2Stoned