简体   繁体   English

json凌空显示错误android

[英]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 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. 以下是logcat中显示的错误。 Please give me some helps. 请给我一些帮助。 Thank you. 谢谢。

You get wrong JSONObject , try this 您得到错误的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();
}

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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