2017-02-27 105 views
0

我有一個JSON檢索數據庫的所有用戶。爲了簡化,我將顯示前兩個用戶:如何在Android上使用Volley檢索沒有名稱的JSON?

[ 
    { 
     "id":"1", 
     "name":"Peter", 
     "age":"25" 
    }, 
    { 
     "id":"2", 
     "name":"Andrew", 
     "age":"32" 
    }, 
    ... 
] 

正如你所看到的,它是沒有名字的,它包含了一些JSONObjects所以我試圖用Volley庫經歷了整個檢索Android這些數據的數組陣列但沒有成功。

下面是我得到的時刻代碼:

RequestQueue queue = Volley.newRequestQueue(getContext()); 

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, new Response.Listener<JSONArray>() { 
    @Override 
    public void onResponse(JSONArray response) { 
     try{ 
      if (response != null) { 
       for(int i = 0; i < response.length(); i++){ 
        JSONObject obj = response.getJSONObject(i); 
       } 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     Log.e("LOG", error.toString()); 
    } 
}); 

queue.add(jsonArrayRequest); 

它總是給我下面的錯誤:

Unhandled exception: org.json.JSONException

我也試圖嘗試檢索之前檢索JSONArrayJSONObject但它不允許我,因爲方法

JSONArray json = response.getJSONArray(0); 

也給了我以前指出的同樣的錯誤。

我找了很多例子,我看不出我的問題在哪裏。

我在做什麼錯?

在此先感謝!

+0

您確定您的服務器響應格式正確嗎?我嘗試了你的面向JSON的代碼部分(硬編碼你輸入的字符串)並且我無法重現你得到的錯誤 –

+0

@GaëtanMaisse在我執行代碼之前,Android Studio給了我那個錯誤,所以我認爲問題是關於代碼的。 –

+0

@ Error404您的代碼執行得很好。你的應用程序只是崩潰,因爲你正在解析(或獲取)JSON不正確。但是,正如第一條評論所述,您的數據必須無效,因爲我們無法重現您的錯誤。 –

回答

1

Cannot resolve constructor ' JsonArrayRequest(int, java.lang.String, anonymous com.android.volley.Response.Listener<org.json.JSONArray>, anonymous com.android.volley.Response.ErrorListener) '

錯誤不會說謊。該請求不接受方法類型。

JsonArrayRequest(
    String url, 
    Response.Listener<JSONArray> listener, 
    Response.ErrorListener errorListener) 

StringRequest另一方面,

StringRequest(
    int method, 
    String url, 
    Response.Listener<String> listener, 
    Response.ErrorListener errorListener) 

關於,

Unhandled exception: org.json.JSONException

response.getJSONObject將拋出該異常,所以你必須使用一個try-catch。

if (response != null) { 
    try { 
     for (int i = 0; ...) { 
      JSONObject jo = response.getJSONObject(i); 
      // use 'jo' 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 
+0

謝謝!你的答案更加完整和直觀。再次感謝你的幫助! –

+0

默認情況下,JsonArrayRequest將使用GET。你不能以任何理由發佈JsonArray ... –

+0

哦,好的。再次感謝你的幫助 :) –

1

您沒有處理JSONException。

試試看。

StringRequest request = new StringRequest(Method.GET, url, 
    new Response.Listener<String>() { 

     @Override 
     public void onResponse(String response) 
      try { 
       JSONArray jsonArray = new JSONArray(response); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jo = jsonArray.getJSONObject(i); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
+2

你爲什麼試圖以String的形式處理你的迴應? –

+0

@ Error404當你的API不能保證返回一個JSON數組時,最好做測試。 StringRequest,我的意思是 –

+0

@ cricket_007,但是我在Android Studio上遇到了錯誤,因爲它不允許我執行我的代碼,所以我認爲在這種情況下它沒有與API相關的任何內容。 –