我在设置JSON文件时遇到了错误

时间:2017-07-24 15:09:55

标签: java android json

到目前为止,我对这一切都很陌生。但是,我试图使用我在网上找到的教程创建一个新闻阅读器应用程序。它涉及JSON文件在线托管并由应用程序访问。

我在JsonObjectRequest上有错误 - 无法解决 请求 - 无法解决。但是有一个导入,它说我可以添加。下载管理器 - 这是我正在尝试实现的正确导入吗?它似乎是这样,但我不是百分百肯定,有人可以澄清一下吗?

Cannot resolve symbol Response.
response.getJSONArray("newsItems"); 
The error is Unhandled exception: org.json.JSONException

请求:

JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET, "https://api.myjson.com/bins/z0lwn", (String)null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        JSONArray newsItems = response.getJSONArray("newsItems");
                        try {
                            for (int i = 0; i < newsItems.length(); i++) {
                                JSONObject temp = newsItems.getJSONObject(i);
                            }

                        }
                    }


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

                    }

                });

1 个答案:

答案 0 :(得分:0)

我打开了api链接,但在json中没有&#34; newsItems&#34;标签...

你可以看到:

{
"0": {
    "image": null,
    "title": "Dhanush walks out of interview over questions on Suchi leaks ",
    "time": "03:39 pm ",
    "date": "24 Jul ",
    "content": "Actor Dhanush walked out of a television interview when the journalist asked him questions about ...",
    "link": "https://www.youtube.com/watch?v=V7EwWHNpiZA&amp;utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"1": {
    "image": null,
    "title": "Froome wins Tour de France 4th time, finishes in 86h 20' 55\" ",
    "time": "03:58 pm ",
    "date": "24 Jul ",
    "content": "British cyclist Chris Froome won his third consecutive and fourth overall Tour de France, finishing ...",
    "link": "https://www.theguardian.com/sport/2017/jul/23/chris-froome-wins-fourth-tour-de-france?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"2": {
    "image": null,
    "title": "Govt launches SHe-box portal for sexual harassment complaint ",
    "time": "03:39 pm ",
    "date": "24 Jul ",
    "content": "The Women and Child Development Ministry launched an online portal SHe-box (sexual harassment ...",
    "link": "http://www.ptinews.com/news/8915808_Govt-launches--SHe-box--portal-for-sexual-harassment.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"3": {
    "image": null,
    "title": "Pak-origin London Bridge terrorist buried in secret: Report ",
    "time": "03:30 pm ",
    "date": "24 Jul ",
    "content": "The Pakistani-origin terrorist behind the London Bridge terror attack, Khuram Butt, has reportedly ...",
    "link": "http://www.hindustantimes.com/world-news/pakistani-origin-london-bridge-terrorist-buried-in-secret-report/story-msuslsd6II94PPiADXlTgP.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"4": {
    "image": null,
    "title": "Donald Trump 1st US President without pet in nearly 150 yrs ",
    "time": "03:37 pm ",
    "date": "24 Jul ",
    "content": "Donald Trump is the first US President without a pet in the White House in nearly 150 years, in ...",
    "link": "http://www.hindustantimes.com/world-news/donald-trump-presidency-who-is-still-missing-from-the-white-house-a-pet/story-q1Nzpf6Vv5IKg42ox43T9K.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"5": {
    "image": null,
    "title": "Girl fined ₹12k for lemonade stand gets dozens of job offers ",
    "time": "03:52 pm ",
    "date": "24 Jul ",
    "content": "A five-year-old girl who was fined £150 (₹12,500) for running a homemade lemonade stall in ...",
    "link": "https://www.youtube.com/watch?v=MNrxH3k11Ms&amp;utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"6": {
    "image": null,
    "title": "Starbucks diner hits robber on head with chair, stops theft ",
    "time": "04:08 pm ",
    "date": "24 Jul ",
    "content": "A Starbucks customer stopped an armed robbery, hitting the suspect over the head with a chair and ...",
    "link": "https://www.youtube.com/watch?v=ug2tQMG8DC0&amp;utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"7": {
    "image": null,
    "title": "3,700-year-old piece of pottery with smiley face discovered ",
    "time": "03:41 pm ",
    "date": "24 Jul ",
    "content": "Archaeologists have discovered a smiley face painted on a 3,700-year-old piece of pottery in the ...",
    "link": "https://www.thequint.com/news/2017/07/23/oldest-emoji-in-the-world?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"8": {
    "image": null,
    "title": "Jio citing 6yr old affidavit to distort facts: Industry body ",
    "time": "03:05 pm ",
    "date": "24 Jul ",
    "content": "The COAI said Reliance Jio, which cited a six-year-old affidavit by TRAI to support its case for ...",
    "link": "https://slack-redir.net/link?url=http%3A%2F%2Fwww.livemint.com%2FIndustry%2Fim8KU1UA2UuhmTypDwfGrK%2FReliance-Jios-reference-of-Trais-IUC-affidavit-attempt-to.html&amp;utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"9": {
    "image": null,
    "title": "United Spirits profit rises 43.6% despite decline in revenue ",
    "time": "03:28 pm ",
    "date": "24 Jul ",
    "content": "Liquor firm United Spirits posted a 43.6% year-on-year increase in profit to ₹62.9 crore for the ...",
    "link": "http://www.livemint.com/Companies/VV1bK7pb91k6UudE2aXxbL/United-Spirits-Q1-profit-grows-436-despite-decline-in-reve.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"10": {
    "image": null,
    "title": "Patanjali takes a very nationalist view of business: Colgate ",
    "time": "04:33 pm ",
    "date": "24 Jul ",
    "content": "Colgate Palmolive's global CEO Ian Cook named Baba Ramdev-led Patanjali Ayurved while talking to ...",
    "link": "http://www.businesstoday.in/current/corporate/patanjali-takes-a-very-nationalist-view-of-its-business-colgate-ceo/story/256989.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"11": {
    "image": null,
    "title": "They have started revolution, couldn't be more proud: Akshay ",
    "time": "03:35 pm ",
    "date": "24 Jul ",
    "content": "Commenting on the Indian women's cricket team finishing as runners-up in the World Cup, actor ...",
    "link": "https://twitter.com/akshaykumar/status/889187425158926336?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"12": {
    "image": null,
    "title": "Directors should decide if they want cuts, not a board: Azmi ",
    "time": "03:59 pm ",
    "date": "24 Jul ",
    "content": "Actress Shabana Azmi has said that filmmakers should decide if they want to cut scenes in their ...",
    "link": "http://www.hindustantimes.com/bollywood/filmmakers-not-censor-board-should-decide-on-cutting-any-scenes-shabana-azmi/story-joWJBBU5UF8lEr7z0OFX9K.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"13": {
    "image": null,
    "title": "Won't associate with films derogatory towards women: Arjun ",
    "time": "04:45 pm ",
    "date": "24 Jul ",
    "content": "Actor Arjun Kapoor has said that he won't affiliate himself with films which are derogatory towards ...",
    "link": "https://www.pinkvilla.com/entertainment/news/383190/arjun-kapoor-i-am-very-proud-i-hail-film-fraternity-i-am-never-apologetic?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"14": {
    "image": null,
    "title": "World's oldest manatee dies a day after 69th birthday ",
    "time": "03:56 pm ",
    "date": "24 Jul ",
    "content": "Snooty, the oldest known manatee in the world, passed away on Sunday, a day after celebrating his ...",
    "link": "https://www.theguardian.com/us-news/2017/jul/23/snooty-worlds-oldest-manatee-dies-69?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"15": {
    "image": null,
    "title": "After snub in 2010, Harmanpreet offered job in Punjab Police ",
    "time": "03:01 pm ",
    "date": "24 Jul ",
    "content": "India's women's cricket team player Harmanpreet Kaur has been offered a job in Punjab Police by CM ...",
    "link": "http://www.aninews.in/newsdetail-Mg/MzI1OTkx/punjab-cm-offers-cricketer-harmanpreet-post-in-punjab-police.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"16": {
    "image": null,
    "title": "Maharashtra approves bank guarantees for 4 Metro projects ",
    "time": "03:00 pm ",
    "date": "24 Jul ",
    "content": "Maharashtra government on Sunday approved bank guarantees and loan-related sanctions for four ...",
    "link": "http://www.hindustantimes.com/mumbai-news/maharashtra-approves-bank-guarantees-loan-sanctions-for-four-metro-projects/story-xvgsWOOTF4U0M1ot4KTOUO.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"17": {
    "image": null,
    "title": "Firms move to court against land acquisition by B'luru Metro ",
    "time": "03:45 pm ",
    "date": "24 Jul ",
    "content": "The completion of Bengaluru's Baiyappanahalli to Whitefield Metro Corridor may get delayed as two ...",
    "link": "http://www.newindianexpress.com/cities/bengaluru/2017/jul/24/firms-unwilling-to-part-with-land-move-court-1632735.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"18": {
    "image": null,
    "title": "Sharif's military meet was at IAF's target during Kargil war ",
    "time": "03:37 pm ",
    "date": "24 Jul ",
    "content": "Two Indian Air Force jets flying near LoC were reportedly about to bomb a Pakistan Army's base ...",
    "link": "http://www.india.com/news/india/kargil-war-iafs-jets-had-sharif-musharraf-on-target-averted-disaster-at-the-last-moment-2345185/?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"19": {
    "image": null,
    "title": "Country dissolves national football team to stop bad results ",
    "time": "03:32 pm ",
    "date": "24 Jul ",
    "content": "Djibouti Football Federation has dissolved the country's senior national team in order to 'stop ...",
    "link": "https://twitter.com/oluwashina/status/888060087243288577?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"20": {
    "image": null,
    "title": "UP launches free power connection schemes for BPL families ",
    "time": "03:47 pm ",
    "date": "24 Jul ",
    "content": "Uttar Pradesh Power Minister Shrikant Sharma on Sunday launched free power connection schemes for ...",
    "link": "http://indiatoday.intoday.in/story/up-govt-launches-free-power-connection-scheme-for-bpl-families/1/1009025.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"21": {
    "image": null,
    "title": "BCCI plans felicitation for Indian women's cricket team ",
    "time": "03:29 pm ",
    "date": "24 Jul ",
    "content": "The Board of Control For Cricket In India (BCCI) is planning a felicitation ceremony for the Indian ...",
    "link": "https://twitter.com/airnewsalerts/status/889401410210545664?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"22": {
    "image": null,
    "title": "Boxer Sachin Siwach wins gold at Commonwealth Youth Games ",
    "time": "03:01 pm ",
    "date": "24 Jul ",
    "content": "World youth boxing champion Sachin Siwach clinched a gold medal at the Commonwealth Youth Games in ...",
    "link": "http://www.ptinews.com/news/8915388_Sachin-strikes-gold-at-Commonwealth-Youth-Games.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"23": {
    "image": null,
    "title": "39-year-old man arrested for flashing at woman in Mumbai ",
    "time": "04:15 pm ",
    "date": "24 Jul ",
    "content": "The Mumbai police on Saturday arrested a 39-year-old man for allegedly flashing at a woman in ...",
    "link": "http://www.hindustantimes.com/mumbai-news/mumbai-man-arrested-for-flashing-woman-says-was-fixing-zip-of-his-trousers/story-wXNFskVuSc8HJ91IyWAfgJ.html?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"24": {
    "image": null,
    "title": "Study finds depression causes changes in brain structure ",
    "time": "02:59 pm ",
    "date": "24 Jul ",
    "content": "A study conducted at the University of Edinburgh has found that depression can result in permanent ...",
    "link": "http://www.biorxiv.org/content/early/2017/06/14/070912?utm_source=inshorts&amp;utm_medium=referral&amp;utm_campaign=fullarticle "
},
"lastID": "zwkvauny-1"

}

编辑: 您可以使用下面的代码片段来迭代json并获取jsonobjects:

 Iterator<?> keys = jsonObject.keys();

 while( keys.hasNext() ) {
      String key = (String)keys.next();
      if ( jsonObject.get(key) instanceof JSONObject ) { //check if the key correspond to a json object
          JSONObject temp=(JSONObject)jsonObject.get(key);
          //do what you have to do
       }
  }