2016-02-28 30 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