I am new to working with APIs and JSON files. I've put together this code by watching some tutorials but am stuck at this point. here is the code:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("articles");
Log.d("1", "onResponse: Success before loop");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
//JSONArray
Log.d("1", "onResponse: Inside the loop");
String title = hit.getString("title");
String imageUrl = hit.getString("imageURL");
String article = hit.getString("content");
String author = hit.getString("author");
String date = hit.getString("time");
mExampleList.add(new ExampleItem(imageUrl, title, article, author, date));
}
mExampleAdapter = new ExampleAdapter(MainActivity.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("1", "onErrorResponse: Error response");
error.printStackTrace();
}
});
mRequestQueue.add(request);
Here is the JSON array that I am receiving:
[
{
"language": "Hindi",
"title": "\"स्वदेशी हथियारों का निर्यात करेगी भारत सरकार, राजनयिक संबंधों का भी करेगी इस्तेमाल\"",
"location": "Dhanbad",
"link": "mysite2.com",
"international": false,
"time": "20\/5\/2020",
"imageURL": "https:\/\/cdn.pixabay.com\/user\/2014\/11\/30\/13-45-12-52_250x250.jpg",
"content": "\"स्वदेशी हथियारों का निर्यात करेगी भारत सरकार, राजनयिक संबंधों का भी करेगी इस्तेमाल\" नई दिल्ली: भारत सरकार ने स्वदेशी हथियारों ( Indigenous Developed weapons) और सैन्य साजो सामान को निर्यात करने की रणनीति बनाई है, जिसमें भारत सरकार अपने राजनयिक संबंधों का भी इस्तेमाल करेगी, रक्षा मंत्रालय से जुड़े सूत्रों ने यह जानकारी दी,रक्षा उत्पादन विभाग के सचिव राजकुमार ने एक वेबिनार में कहा कि भारत सरकार मित्र देशों के राजनयिकों से संपर्क स्थापित कर यह जानने की रणनीति बना रही है कि उन्हें किस तरह के हथियारों और रक्षा सौदों की जरूरत है. ताकि भारतीय उत्पादक उनके हिसाब से रक्षा सामग्री का निर्माण कर सकें। कुमार ने कहा कि भारत सरकार अपने रक्षा प्रतिनिधियों, दूतावासों और राजनयिक चैनलों का इस्तेमाल मेक इन इंडिया के तहत बनाए जाने वाले हथियारों के निर्यात को बढ़ाने में करेगी।",
"author": "Sova"
}
]
I am unable to figure out the issue here. I know that there is a very minor issue here. I hope you can help me out here.
Here is the full JSON:
{
"articles": {
"PHcgUZAwEHRlVZKXtxph": {
"international": true,
"imageURL": "https://cdn.pixabay.com/user/2014/11/30/13-45-12-52_250x250.jpg",
"location": "all",
"link": "mysite2.com",
"time": "23/4/58",
"language": "English",
"author": "Rze",
"content": "Last Article I Promise",
"title": "Lorem Ipsum v3"
},
"PeIutXkKrL4Fftku6mKf": {
"language": "English",
"content": "Last Article I Promise",
"author": "USAID",
"locatoin": "Lucknow",
"time": "24/07/2020",
"imageURL": "https://cdn.pixabay.com/user/2014/11/30/13-45-12-52_250x250.jpg",
"link": "mysite2.com",
"title": "Lorem Ipsum v3"
},
"bmhhMM0meX98dP8JAJWk": {
"location": "Lucknow",
"title": "Lorem Ipsum v3",
"language": "English",
"imageURL": "https://cdn.pixabay.com/user/2014/11/30/13-45-12-52_250x250.jpg",
"time": "24/5/2001",
"content": "Last Article I Promise",
"international": false,
"link": "mysite2.com",
"author": "Mohd Usaid"
},
"p8ayMlH9i6HUVqaQUiKW": {
"international": false,
"title": "\"स्वदेशी हथियारों का निर्यात करेगी भारत सरकार, राजनयिक संबंधों का भी करेगी इस्तेमाल\"",
"language": "Hindi",
"link": "mysite2.com",
"content": "\"स्वदेशी हथियारों का निर्यात करेगी भारत सरकार, राजनयिक संबंधों का भी करेगी इस्तेमाल\" नई दिल्ली: भारत सरकार ने स्वदेशी हथियारों ( Indigenous Developed weapons) और सैन्य साजो सामान को निर्यात करने की रणनीति बनाई है, जिसमें भारत सरकार अपने राजनयिक संबंधों का भी इस्तेमाल करेगी, रक्षा मंत्रालय से जुड़े सूत्रों ने यह जानकारी दी,रक्षा उत्पादन विभाग के सचिव राजकुमार ने एक वेबिनार में कहा कि भारत सरकार मित्र देशों के राजनयिकों से संपर्क स्थापित कर यह जानने की रणनीति बना रही है कि उन्हें किस तरह के हथियारों और रक्षा सौदों की जरूरत है. ताकि भारतीय उत्पादक उनके हिसाब से रक्षा सामग्री का निर्माण कर सकें। कुमार ने कहा कि भारत सरकार अपने रक्षा प्रतिनिधियों, दूतावासों और राजनयिक चैनलों का इस्तेमाल मेक इन इंडिया के तहत बनाए जाने वाले हथियारों के निर्यात को बढ़ाने में करेगी।",
"location": "Dhanbad",
"imageURL": "https://cdn.pixabay.com/user/2014/11/30/13-45-12-52_250x250.jpg",
"author": "Sova",
"time": "20/5/2020"
}
}
}
articlesin your json?[, indicating an array, the response is aJSONArray, not aJSONObject, so change tonew Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) {. --- Don't know how you came up withgetJSONArray("articles")when there are no"articles"anywhere in the JSON response.