Creating a Twitter API using java

This is just a starters app. There are many beautiful apps available but the more great the app is; more are the complications involved. This post is just for the beginners to familiarise with API development.
Most social networks use API calls to link to their API's to exchange data. We will use the same API calls to connect to them which they have "open sourced" to the developers for use.
Firstly visit the page http://api.twitter.com/statuses/public_timeline.html
This is a public twitter page which can be accessed without any login. It gives the recent tweets of people.
Now lets say we are writing a web app to display just the recent public tweets ( without any name or other information).
Now copy this to the address bar : http://api.twitter.com/1/statuses/public_timeline.json
This is same the same page you viewed but in .json(JavaScript Object Notation) format.
You can also use  https://apigee.com/console/twitter  to see the .json response and request method.

Short note on JSON :

JSON is syntax for storing and exchanging text information. Much like XML.
JSON is smaller than XML, and faster and easier to parse.
(courtesy http://www.w3schools.com/json/default.asp )

As a app developer our duty is to parse through this and find the required feilds.In this app our aim is to find the messages.Now to obtain the json file into an object we can use the following java code :

 URL url  =  new URL ("http://api.twitter.com/1/statuses/public_timeline.json");
 BufferedReader reader = new BufferedReader (new InputStreamReader( url.openStream( ) ) );


Now 'reader' is the object bound to the required .json file. Next we have to parse this file to find the tweet messages. If you take a close look at the http://api.twitter.com/1/statuses/public_timeline.json file you will notice that the whole file is in [ ]. This implies that the whole page is a single record.We have json libraries for all languages which helps us to parse files. We also notice that the tweet messages are refered by keyword "text". The json file is a record of  key:value pairs. So if we find the keyword as 'text' we know the value following it is our tweet message.
Before parsing the file we need an object which has the key to be searched and a variable that can hold the corresponding value.So we write a class, say KeyFinder implementing ContentHandler.It will have functions to set the key to be searched, setting the value etc. So we set key to be searched as "text" and if it is present the value corresponding to it is stored and printed.

The json_simple dependency  jar file and its usage can be viewed at http://code.google.com/p/json-simple/ . The examples there should help you understand KeyFinder class better. Donot forget to import necessary packages and to save the jar files in the same folder as your program. Give it a try....

Creating a Twitter API using java

This is just a starters app. There are many beautiful apps available but the more great the app is; more are the complications involved. This post is just for the beginners to familiarise with API development.
Most social networks use API calls to link to their API's to exchange data. We will use the same API calls to connect to them which they have "open sourced" to the developers for use.
Firstly visit the page http://api.twitter.com/statuses/public_timeline.html
This is a public twitter page which can be accessed without any login. It gives the recent tweets of people.
Now lets say we are writing a web app to display just the recent public tweets ( without any name or other information).
Now copy this to the address bar : http://api.twitter.com/1/statuses/public_timeline.json
This is same the same page you viewed but in .json(JavaScript Object Notation) format.
You can also use  https://apigee.com/console/twitter  to see the .json response and request method.

Short note on JSON :

JSON is syntax for storing and exchanging text information. Much like XML.
JSON is smaller than XML, and faster and easier to parse.
(courtesy http://www.w3schools.com/json/default.asp )

As a app developer our duty is to parse through this and find the required feilds.In this app our aim is to find the messages.Now to obtain the json file into an object we can use the following java code :

 URL url  =  new URL ("http://api.twitter.com/1/statuses/public_timeline.json");
 BufferedReader reader = new BufferedReader (new InputStreamReader( url.openStream( ) ) );


Now 'reader' is the object bound to the required .json file. Next we have to parse this file to find the tweet messages. If you take a close look at the http://api.twitter.com/1/statuses/public_timeline.json file you will notice that the whole file is in [ ]. This implies that the whole page is a single record.We have json libraries for all languages which helps us to parse files. We also notice that the tweet messages are refered by keyword "text". The json file is a record of  key:value pairs. So if we find the keyword as 'text' we know the value following it is our tweet message.
Before parsing the file we need an object which has the key to be searched and a variable that can hold the corresponding value.So we write a class, say KeyFinder implementing ContentHandler.It will have functions to set the key to be searched, setting the value etc. So we set key to be searched as "text" and if it is present the value corresponding to it is stored and printed.

The json_simple dependency  jar file and its usage can be viewed at http://code.google.com/p/json-simple/ . The examples there should help you understand KeyFinder class better. Donot forget to import necessary packages and to save the jar files in the same folder as your program. Give it a try....