2016-01-23 46 views
2

發出請求後,所調用的抽象偵聽器是onErrorResponse,令我驚訝的是,錯誤消息是我從我的服務器(具有正確數據的JSON)想要的回調。Android Volley JsonRequest接收到錯誤的返回

奇怪的是一個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:值{ 「數據」:{ 「場所」:[{ 「CREATED_BY」:1,」電話「:」(85)3246-3834「,」teste「:1453583589,」neighborhood「:」JoaquimTávora「,」last_promotion「:」2015-10-06 12:42:01「,」days_last_promotion「:110, 「id」:2,「distance」:0,「updated_at」:「2015-10-06 12:42:01」,「address」:「Av。Barãode Studart,2675」,「name」:「SãoLuiz 「,」updated_by「:1,」created_at「:」2015-05-31 19:48:58「,」active「:true,」longitude「:」 - 38.510342「,」latitude「:」 - 3.746922「}, {「created_by」:3,「phone」:「(85)4008-2444」,「teste」:1453583589,「neighborhood」:「Messejana」,「last_promotion」:「2016-01-22 13:45:30」 「days_last_promotion」:2 「ID」:96, 「距離」:0,「U pdated_at「:」2016-01-22 13:45:30「,」地址「:」Av。 Frei Cirilo,4290「,」name「:」Pinheiro「,」updated_by「:3,」created_at「:」2015-12-24 14:27:29「,」active「:true,」longitude「:」 - 38.495841 「,」緯度「:」 - 3.821112「},{」created_by「:3,」phone「:」「,」teste「:1453583589,」neighborhood「:」Vila Peri「,」last_promotion「 21 19:06:16「,」days_last_promotion「:2,」id「:140,」distance「:0,」updated_at「:」2016-01-21 19:06:16「,」address「:」RuaCônego de Castro,3859「,」name「:」Cometa「,」updated_by「:null,」created_at「:」2016-01-21 18:21:41「,」active「:true,」longitude「:」 - 38.587981 「,」緯度「:」 - 3.800781「}],」establishments_total「:134},」success「:」YES「,」time「:」2016-01-23 18:13:09「} org.json類型.JSONObject不能轉換到JSONArray

回答

1

您正在使用JsonArrayRequest當你得到一個JSONObject從服務器的響應使用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); 
}