Volley请求总是进入Error Listener

时间:2016-02-28 18:07:37

标签: java android json listview android-fragments

JAVA代码: -

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://sikkimexpress.itstunner.com/api/homenewslist/topnews", new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray jsonArray = response.getJSONArray("HomeNews");

                                for (int i = 0; i<jsonArray.length();i++){
                                    JSONObject homenews = jsonArray.getJSONObject(i) ;

                                    String newsId = homenews.getString("NewsId");
                                    String dateTime = homenews.getString("DateTime");
                                    String newsType = homenews.getString("NewsType");
                                    String title = homenews.getString("Title");
                                    String description = homenews.getString("Description");
                                    String mainImageURL = homenews.getString("MainImageThumbnail");

                                    System.out.println("Result:- " + newsId + " " + dateTime + " " + newsType + " " + title + " " + description + " " + mainImageURL);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },

                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {


                            Log.e("VOLLEY", "ERROR");
                        }
                    });


            requestQueue.add(jsonObjectRequest);

JSON: -

{
  "Status": 0,
  "Message": "Sucess"
  "HomeNews": [
    {
      "NewsId": 13,
      "DateTime": null,
      "NewsType": "latest",
      "Title": "Jat quota row: Haryana CM calls all-party meet as stir continues",
      "Description": "<p>Jat students block the Rohtak-Delhi road in Rohtak as part of the quota agitation. (Manoj Dhaka/HindustanTimes ). Share. Share. Share.</p>",
      "MainImageThumbnail": "http://ste.india.com/sites/default/files/2016/02/19/461939-haryana-jat-protest.jpg",
      "ReferenceURL": ""
    },
    {
      "NewsId": 15,
      "DateTime": null,
      "NewsType": "latest",
      "Title": "Sports quiz of the week",
      "Description": "<p>Which snooker player competing at the Welsh Open said: \"I get so bored at these tournaments. Maybe they should stick an adult creche here to keep us entertained because I just want to go home now.\r\n\r\n\"Today I've had a haircut, I've been in the Chinese, I've been for a coffee, I went to Bill's cafe for a couple of hours, I've had a kip, I went to Marks' and bought a bit of grub. What a day. It's certainly not glamorous.\"?</p>",
      "MainImageThumbnail": "http://news.files.bbci.co.uk/vj/live/idt-images/quizzes-sports_weekly_quiz_week7/Snooker_comp_getty_k4960.jpg",
      "ReferenceURL": ""
    },
]

}

我想解析json,但总是来到 Response.ErrorListener 。如果status = 0且消息成功,则将检索NewsId,DateTime,title,description的值。请帮忙吗?我获得了互联网的许可,我检查了网址并且它正在运行。

1 个答案:

答案 0 :(得分:2)

我能得到正确的答复。如果您收到错误,请记录

Log.e("VOLLEY", error.getMessage());

一个问题可能是您没有为JsonObjectRequest使用正确的构造函数。理想情况下你应该使用,第三个参数jsonRequest可以作为null传递给你。

com.android.volley.toolbox.JsonObjectRequest.JsonObjectRequest(int
 method, String url, JSONObject jsonRequest, Listener<JSONObject>
 listener, ErrorListener errorListener)

其他一切都处于可行状态。