2016-01-08 57 views
0
 JsonArrayRequest movieReq = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        Log.d(TAG, response.toString()); 
        hidePDialog(); 

        // Parsing json 
        for (int i = 0; i < response.length(); i++) { 
         try { 

          JSONObject obj = response.getJSONObject(i); 
          Patient patient = new Patient(); 
          patient.setTitle(obj.getString("id")); 
          patient.setThumbnailUrl(obj.getString("image")); 

          patientList.add(patient); 

         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 

        // notifying list adapter about data changes 
        // so that it renders the list view with updated data 
        adapter.notifyDataSetChanged(); 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        hidePDialog(); 

       } 
      }); 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(movieReq); 
} 

logcat的JSON凌空示值誤差的Android

01-07 07:27:29.294 7938-7938/info.androidhive.customlistviewvolley D/MainActivity: [{"id":"g"},{"image":"http:\/\/192.168.0.101\/test\/1.png"}] 
01-07 07:27:29.312 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for image 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at org.json.JSONObject.get(JSONObject.java:389) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at org.json.JSONObject.getString(JSONObject.java:550) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:72) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:59) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at android.os.Handler.handleCallback(Handler.java:739) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at android.os.Looper.loop(Looper.java:135) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5254) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at java.lang.reflect.Method.invoke(Method.java:372) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for id 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at org.json.JSONObject.get(JSONObject.java:389) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at org.json.JSONObject.getString(JSONObject.java:550) 
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err:  at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:71) 

我可以從數據庫中獲取數據,但爲何仍表明,對於像沒有價值? 我檢查了網址是否正確以獲取照片。以下是logcat中顯示的錯誤。請給我一些幫助。謝謝。

回答

2

你錯了的JSONObject,試試這個

try { 
    JSONObject objId = response.getJSONObject(0); 
    JSONObject objImage = response.getJSONObject(1); 
    Patient patient = new Patient(); 
    patient.setTitle(objId.getString("id")); 
    patient.setThumbnailUrl(objImage.getString("image")); 

    patientList.add(patient); 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 
0

看看你的反應,試試這個

JSONObject obj = response.getJSONObject(0); 
JSONObject obj1 = response.getJSONObject(1); 
Patient patient = new Patient(); 
patient.setTitle(obj.getString("id")); 
patient.setThumbnailUrl(obj1.getString("image")); 
+0

爲什麼你需要使用兩個一個JSONObjects?謝謝 –

+0

根據你的迴應,你可以從0(零)位置和1(第一位置)的圖像獲得ID。 –