2016-02-28 43 views
0

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。如果状态= 0且消息成功,则将检索NewsId,DateTime,标题,描述的值。有人可以帮忙吗?我接受了互联网的许可,并检查了URL并且它正在工作。

+0

url响应是XML而不是JSON – Yazan

+0

如果你有邮递员,那么你会看到json – user312456

+0

谁告诉你我没有使用邮递员? – Yazan

回答

1

我能够得到正确的答复。如果您收到错误,如何登录

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

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

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

否则一切都处于可行的状态。

+0

嘿巴迪非常感谢,但我得到** java.net.UnknownHostException **。如何解决它? – user312456

+0

然后你需要''''''''''''''''''''''''''''''请尝试在浏览器中打开其他网站,看看您是否能够做到这一点。 – Mithun

+0

现在它的权利,但我不能设置列表中的数据 – user312456