0

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"
        }
    }
}
2
  • where is articles in your json? Commented Oct 9, 2020 at 8:03
  • Since the JSON starts with [, indicating an array, the response is a JSONArray, not a JSONObject, so change to new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) {. --- Don't know how you came up with getJSONArray("articles") when there are no "articles" anywhere in the JSON response. Commented Oct 9, 2020 at 8:24

1 Answer 1

1

Since you mentioned that this is a custom JSON. You need to change the JSON itself to make your code working.

{
  "articles":[ //this needs to be array not object
     {
        "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"
     },
     {
        "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"
     },
     {
        "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"
     },
     {
        "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"
     }
  ]
}

Make your articles an array instead of object. Try to remove those random key names from your article object names. Your java code is fine its the custom JSON which has problem in it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.