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
Elizabeth Joseph
1,969 PointsCant get Code Challenge to work
Filling our String array and Creating an Adapter code challenge 1/3
Is something wrong with this code?
JSONArray jsonVideos = jsonData.getJSONArray("videos");
String[] titles = new String[jsonVideos.length()];
for(int i =0; i <jsonVideos.length() ;i++)
{
JSONObject post = jsonVideos.getJSONObject(i);
String vtitle =post.getString("title");
vtitle = Html.fromHtml(vtitle).toString();
titles[i]= vtitle;
}
5 Answers
Ben Jakuben
Treehouse TeacherYou're too good! The Code Challenge isn't expecting the Html.fromHtml() call. We have a few tricks in place to do some of our validation. Just remove that line and you're good to go. I'll see about fixing the Code Challenge engine to account for what you did, which makes sense.
Elizabeth Joseph
1,969 PointsHi Ben,
Thanks for your response on that one, but I had a similar issue with another code challenge as well. Parsing Data returned in JSONFormat. task 3/3 any combination of comma and space doesnt work. "," +" " OR ", "
The code im using is as follows: String name =jsonData.getString("name"); String publisher =jsonData.getString("publisher"); String language =jsonData.getString("language");
JSONArray jsonBooks = jsonData.getJSONArray("books");
for(int i =0 ; i<jsonBooks.length(); i++) { JSONObject post = jsonBooks.getJSONObject(i); String title =post.getString("title"); String pages =post.getString("pages"); Log.i("CodeChallenge", title + " , " + pages); }
Ben Jakuben
Treehouse TeacherThis one is intentionally tricky. "pages" is an integer, so you need to use a different method than "getString". It trips a lot of people up, though, so I will likely change it.
ms2030
2,973 PointsSo, what am I doing wrong on this one? BTW, thanks for the hint about the integer but
String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String language = jsonData.getString("language");
JSONArray jsonBooks = jsonData.getJSONArray("books");
for (int i = 0; i< jsonBooks.length(); i++) {
JSONObject jsonBook = jsonBooks.getJSONObject(i);
String title = jsonBook.getString("title");
int pages = jsonResponse.getInt("pages");
Log.i("CodeChallenge", title + ", " + pages);
}
Ben Jakuben
Treehouse TeacherSorry for the late reply! In this one you had jsonResponse trying to get the pages. Looks like you fixed that below...
ms2030
2,973 Pointsreplaced the jsonResponse with jsonBook but still not it...
// JSONObject 'jsonData' was loaded from data.json
String name = jsonData.getString("name");
String publisher = jsonData.getString("publisher");
String language = jsonData.getString("language");
int i;
JSONArray jsonBooks = jsonData.getJSONArray("books");
for (int i = 0; i< jsonBooks.length(); i++) {
JSONObject jsonBook = jsonBooks.getJSONObject(i);
String title = jsonBook.getString("title");
int pages = jsonBook.getInt("pages");
Log.i("CodeChallenge", title + ", " + pages);
}
Ben Jakuben
Treehouse TeacherAgain, sorry for the late response! Hopefully you got past this already. :) In this latest code snippet you are declaring int i twice, which should give a compiler error as it can only be declared one time.