2017-08-26 68 views
1

當我打電話給我的服務web時,我有這個異常:org.json.JSONException:Value [{}] type org.json.JSONArray不能被轉換爲JSONObject的org.json.JSONException:類型org.json.JSONArray的值[{}]無法轉換爲JSONObject

result

這是我的源代碼:

JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, url + editText.getText().toString(), null, 
         new Response.Listener<JSONObject>() { 
          @Override 
          public void onResponse(JSONObject response) { 

           try { 

           JSONArray missions = response.getJSONArray("Value"); 

           for(int i=0;i<missions.length();i++){ 
             // Get current json object 
             JSONObject mission = missions.getJSONObject(i); 

             // Get the current student (json object) data 
             Long idMission = mission.getLong("idMission"); 
             String etatMission = mission.getString("etatMission"); 

             text.append(idMission+" " + etatMission); 
             text.append("\n\n"); 
             } 
           }catch (JSONException e) { 
            e.printStackTrace(); 
           } 

          } 
         }, 
         new Response.ErrorListener() { 
          @Override 
          public void onErrorResponse(VolleyError error) { 
           text.setText(error.getMessage()); 
          } 
         } 
       ); 
       requestQueue.add(obreq); 

回答

0

的ansewer很簡單:

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

如果您從API得到響應是JsonArray那麼你應該使用這樣的:

public JsonArrayRequest(int method, String url, JSONObject jsonRequest, 
    Listener<JSONArray> listener, ErrorListener errorListener) { 
super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), 
    listener, errorListener); 
} 
相關問題