java - Volley 请求总是进入错误监听器

标签 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 的值。有人可以帮忙吗?我获得了互联网的许可,并检查了 URL,它正在工作。

最佳答案

我能够得到正确的响应。如果出现错误,请记录日志

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)

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

关于java - Volley 请求总是进入错误监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35686260/

相关文章:

java - 根据字符串数组的顺序重新排序 ArrayList - Java

java - XWPFDocument - getPageCount

android - 将 ?attr/selectableItemBackground 添​​加到 View 并设置背景颜色

android - 如何简单地打开目录/文件夹?

json - 粘贴到预先格式化的 JSON block 中

php - 在 composer.json 中运行命令行命令

java - 在 Android 中创建自定义首选项屏幕

java - 如何通过命令行启动spring-boot应用程序?

java - Android : Maps not zooming in to my location, 沿 Y 坐标随机位置

python - Pandas:将组转换为 json 列表,而不使用 groupby 或 apply