2016-01-08 54 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。 –