Android Volley JsonRequest收到错误的回复

时间:2016-01-23 21:30:34

标签: android json request android-volley

在发出请求之后,调用的volley侦听器是onErrorResponse,我惊讶地发现错误消息是我想要从我的服务器回调的,一个带有正确数据的json。

有什么奇怪的是parseError但是如果我在onResponse监听器中进行解析,那么解析错误是怎么回事。

我是android的新手。

我的方法:

public void getAllEstablishmentsRequest(){

    String url = "http://myServer.com/something";

    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonArrayRequest request = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>(){
                @Override
                public void onResponse(JSONArray response) {
                    try {

                        JSONObject object = new JSONObject(response.toString());
                        JSONArray Jarray = object.getJSONArray("data");

                        for (int i = 0; i < Jarray.length(); i++) {
                            JSONObject Jasonobject = Jarray.getJSONObject(i);
                        }

                    } catch (JSONException e){
                        Log.e("Volley", "Error JSON");
                    }
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.e("Volley", "Error - requisição das campanhas de Eutrofização");
                    System.out.println(error);
                }
            }
    );
    requestQueue.add(request);
}

错误:com.android.volley.ParseError:org.json.JSONException:Value {“data”:{“establishment”:[{“created_by”:1,“phone”:“(85)3246-3834” ,“teste”:1453583589,“邻居”:“JoaquimTávora”,“last_promotion”:“2015-10-06 12:42:01”,“days_last_promotion”:110,“id”:2,“距离”:0 ,“updated_at”:“2015-10-06 12:42:01”,“地址”:“Av.BarãodeStudart,2675”,“name”:“SãoLuiz”,“updated_by”:1,“created_at” :“2015-05-31 19:48:58”,“有效”:真实,“经度”:“ - 38.510342”,“纬度”:“ - 3.746922”},{“created_by”:3,“手机”: “(85)4008-2444”,“teste”:1453583589,“邻居”:“Messejana”,“last_promotion”:“2016-01-22 13:45:30”,“days_last_promotion”:2,“id”: 96,“距离”:0,“updated_at”:“2016-01-22 13:45:30”,“地址”:“Av.Frei Cirilo,4290”,“名称”:“Pinheiro”,“updated_by”: 3,“created_at”:“2015-12-24 14:27:29”,“活跃”:真实,“经度”:“ - 38.495841”,“纬度”:“ - 3.821112”},{“created_by”:3 ,“电话”:“”,“teste”:1453583589,“邻居”:“Vila Peri”,“last_promotion”:“2016-01-21 19:06:16”,“days_last_promotion”:2,“id”: 140,”距离“:0,”updated_at“:”2016-01-21 19:06:16“,”地址“:”RuaCônegodeCastro,3859“,”name“:”Cometa“,”updated_by“:null,” created_at“:”2016-01-21 18:21:41“,”active“:true,”longitude“:” - 38.587981“,”latitude“:” - 3.800781“}],”establishmentments_total“:134},”成功“:”是“,”时间“:”2016-01-23 18:13:09“}类型org.json.JSONObject无法转换为JSONArray

1 个答案:

答案 0 :(得分:1)

当您从服务器获取JsonObject作为响应时,您正在使用JsonArrayRequest。请改用JsonObjectRequest。像这样:

public void getAllEstablishmentsRequest(){

   String url = "http://myServer.com/something";

    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JsonObjectRequest request = new JsonObjectRequest(url,
            new Response.Listener<JSONObject>(){
                @Override
                public void onResponse(JSONObject object) {
                    try {
                        JSONArray Jarray = object.getJSONArray("data");

                        for (int i = 0; i < Jarray.length(); i++) {
                            JSONObject Jasonobject = Jarray.getJSONObject(i);
                        }

                    } catch (JSONException e){
                        Log.e("Volley", "Error JSON");
                    }
                }
            },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    Log.e("Volley", "Error - requisição das campanhas de Eutrofização");
                    System.out.println(error);
                }
            }
    );
    requestQueue.add(request);
}