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

pdiaz
pdiaz
16,073 Points

Can someone please help me (Blog reader App)

How do you get this data: http://blog.teamtreehouse.com/api/get_recent_summary/ from another website or blog?

Sante Kotturi
Sante Kotturi
7,434 Points

I presume you want to get data from another website or blog and use it in your Blog Reader app?

(technically another website wont have the data the treehouse example site has, thus I wanted to clarify your question)

I believe there are two answers:

  1. The easy way: the site you're trying to access the data from hopefully has an API. If it does then you need to read the documentation for accessing the API. If you're trying to pull data from a wordpress page: http://codex.wordpress.org/WordPress_APIs

  2. The hard way: If you're target site doesnt haven an API then you'll need to some pretty fancy hacking to convert the data from that site into the data structure needed to pull it into your Blog Reader App.

This post is trying to do the same thing I believe: https://teamtreehouse.com/forum/how-to-convert-websites-into-json-format-for-building-a-blog-reader-app-2

Having an API is REALLY NICE! It takes more work for the people who are building a site but adds a lot of fun for developers like yourself.

Basically, let's take the apple.com website. When you go to apple.com your web browser sends a request to apple's servers asking for (let's just take the example of the header bar) all the different types of products Apple produces (Store, Mac, iPhone, Watch, iPad, iPod, iTunes, Support). This data then gets rendered in HTML and displayed. However, if apple had an API, then you could go to apple.com/api/products and it would send you a list of all the apple products like: `products: {"Store", "Mac", "iPhone", "Watch", "iPad", "iPod", "iTunes", "Support"}. This wouldn't be very pretty to look but it would help people build fake knock-off apple sites....something Apple doesnt want. BUT maybe this helps give you the idea.

Without an API then you need to grab all the data from the apple site, figure out the html elements that contain the data you want, in this case (written in javascript):

This isn't for the faint of heart and definitely not for someone with some solid html, javascript and jquery experience. If you're not comfy with these then please stick to sites with APIs.

EDIT: this actually works on the treehouse site.

// Note this does actually work on the treehouse page
var pages = document.getElementById("main-pages");   // get all the desired elements by the parent class name 
var myList = new Array();     // make a new array to store all our data in, we'll turn this into JSON at the end 
for(var i=0; i< pages.children.length; i++) {      // iterate over each child element
   var holderVar = pages.children[i];          // get the first child element
   var myPage = {}
   myPage.title = holderVar.getAttribute("id"); 
   myList.push(myPage);
} 
var myJSONlist = JSON.stringify(myList);

Have fun!

pdiaz
pdiaz
16,073 Points

Okay, so basically if the website does not supply an api I can't construct a blog reader app?

Sante Kotturi
Sante Kotturi
7,434 Points

If the website you're trying to access doesn't have an API then I would use the one treehouse suggest to complete the blog reader app. Or find another site with an API.

Sante Kotturi
Sante Kotturi
7,434 Points

@Leonardo, this seems to have more useful info (tools that do the JSON conversion for you): https://teamtreehouse.com/forum/how-to-convert-websites-into-json-format-for-building-a-blog-reader-app