Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

JSON OBJECTS in App like a WeatherApp

Hi. I am building an app like a weather app but i am using treehouse api. I receive this error:

07-08 20:06:42.935 7022-7098/com.example.gonzalo.wordpressreader E/MainActivity: Exception caught: org.json.JSONException: No value for posts at

And this is my code:

    private Posts getCurrentDetails(String jsonData) throws JSONException {

JSONObject objeto = new JSONObject();

        JSONObject posts = objeto.getJSONObject("posts");
        Posts currentPost = new Posts();
        currentPost.setmId(posts.getString("id"));
        currentPost.setmUrl(posts.getString("url"));
        currentPost.setmTitle(posts.getString("title"));
        currentPost.setmDate(posts.getString("date"));


        currentPost.setmAuthor(posts.getString("author"));
currentPost.setmThumbnail(posts.getString("thumbnail"));

        return currentPost;
    }

i dont understand why it doesnt work

Sorry for my english. I am from Argentina.

7 Answers

Kourosh Raeen
Kourosh Raeen
23,733 Points

posts is a JSONArray containing JSON objects. You cannot call getInt() or getString() on posts. You need to loop through that array, get the individual JSON objects and call those methods on those objects:

for (int i = 0 ; i < posts.length(); i++) {
    JSONObject post = posts.getJSONObject(i);
    currentPost.setmId(post.getInt("id"));
    currentPost.setmTitle(post.getString("title"));
    // etc.
}
Kourosh Raeen
Kourosh Raeen
23,733 Points

The problem might be in this line of code:

JSONObject objeto = new JSONObject();

I think it should be:

JSONObject objeto = new JSONObject(jsonData);

Not really... i added jsonData but nothing happens. :(

07-08 22:17:55.964 7921-10877/com.example.gonzalo.wordpressreader E/MainActivity: Exception caught: org.json.JSONException: Value [{"id":26261,"url":"http:\/\/blog.teamtreehouse.com\/codingwisdom-start-beginner","title":"#CodingWisdom: Where to Start as a Beginner","date":"2016-07-08 02:55:00","author":"Faye Bridge","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2016\/07\/148-150x150.jpeg"},{"id":26288,"url":"http:\/\/blog.teamtreehouse.com\/new-course-roundup-15","title":"New Course Roundup: Android, iOS, ASP.NET MVC, REST API & PostCSS","date":"2016-07-04 09:33:10","author":"Faye Bridge","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2016\/07\/Ben-J-150x150.png"},{"id":26283,"url":"http:\/\/blog.teamtreehouse.com\/adams-story-becoming-a-professional-classical-musician-and-web-developer","title":"Meet Adam, a Professional Classical Musician and Web Developer","date":"2016-06-30 08:00:04","author":"Faye Bridge","thumbnail":"http:\/\/blog.teamtreehouse.com\/wp-content\/uploads\/2016\/06\/Adam-Hansen-150x150.jpg"},{"id":2

Kourosh Raeen
Kourosh Raeen
23,733 Points

But jsonData is a String not a JSONArray. What line of code is the error referring to and what is the exact error message?

because i tried using jsonArray... the api represent the information using this mode:

<code> posts: [ { id: 26261, url: "http://blog.teamtreehouse.com/codingwisdom-start-beginner", title: "#CodingWisdom: Where to Start as a Beginner", date: "2016-07-08 02:55:00", author: "Faye Bridge", thumbnail: "http://blog.teamtreehouse.com/wp-content/uploads/2016/07/148-150x150.jpeg" },

]

</code>

Hi Gonzaloº,

If the posts are stored in a json array then I think your code would need to be more like this:

JSONObject objeto = new JSONObject(jsonData);
JSONArray posts = objeto.getJSONArray("posts");

If this doesn't help then post the url that you're getting the data from so we can see what it looks like.

I dont understand in the Android Development Documentation tells:

JSONObject:

A modifiable set of name/value mappings. Names are unique, non-null strings. Values may be any mix of JSONObjects, JSONArrays, Strings, Booleans, Integers, Longs, Doubles or NULL. Values may not be null, NaNs, infinities, or of any type not listed here.

I tried using in my code:

JSONObject objeto = new JSONObject(jsonData);
JSONArray posts = objeto.getJSONArray("posts");

But this stopped serving:

currentPost.setmId(posts.getInt("id"));
currentPost.setmUrl(posts.getString("url"));
currentPost.setmTitle(posts.getString("title"));
currentPost.setmDate(posts.getInt("date"));
currentPost.setmAuthor(posts.getString("author"));
currentPost.setmThumbnail(posts.getString("thumbnail"));

Which is the way I use?

Thank you my friend. I solved the problem with your code. And adding this (ups!):

ButterKnife.bind(this); In onCreate method Ja.

Solved!