简体   繁体   中英

json volley showing error android

        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

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)

I can get data from the database but why does it still show that there is no value for the image? I have checked that the url is right to get the photo. The following things are the error showed in the logcat. Please give me some helps. Thank you.

You get wrong JSONObject , try this

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();
}

Look at your response and try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM