import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class GetTweets { public static void main(String[] args) { try { URL url = new URL("https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=srikanthpragada&count=5"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(url.openStream()); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile("/statuses/status"); NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nl.getLength(); i++) { System.out.printf("%s\n%s\n\n", getElementValue(nl.item(i),"text"), getElementValue(nl.item(i),"created_at")); } } catch (Exception ex) { ex.printStackTrace(); } } public static String getElementValue(Node parent, String name) { NodeList list = parent.getChildNodes(); for ( int i = 0; i < list.getLength() ; i ++) { if ( list.item(i).getNodeName().equals(name)) return list.item(i).getFirstChild().getNodeValue(); } return null; } }
import java.util.List; import twitter4j.Paging; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterFactory; public class GetTweetsWithTwitter4J { public static void main(String[] args) { try { Twitter twitter = new TwitterFactory().getInstance(); Paging p= new Paging(); p.setCount(5); // specify you want only 5 most recent entries List<Status> statuses = twitter.getUserTimeline("srikanthpragada",p); // screenname is srikanthpragada for (Status status : statuses) { System.out.printf("%s\n%s\n\n",status.getText(), status.getCreatedAt()); // get text and created_at attributes } } catch (Exception ex) { ex.printStackTrace(); } } }
twitterclient.html <html> <head> <title>Twitter Client </title> <script language="javascript" src="jquery-1.6.1.js"></script> <script language="javascript"> function getTweets() { url = "https://api.twitter.com/1/statuses/user_timeline.json?callback=?"; $.getJSON(url, { screen_name: $("#screen_name").val(), count: "5" }, displayResult); } function displayResult(data) { var out = ''; for (var i = 0; i < data.length; i++) { out += '<li>' + data[i].text + '<br/> - ' + data[i].created_at + "</li>"; } $("#tweets").html(out); } </script> </head> <body> <h2>Tweets</h2> Enter Screen Name : <input type="text" id="screen_name" size="20" value="srikanthpragada"/> <p/> <input type="button" value="Get Tweets" onclick="getTweets()" /> <p/> <ul id="tweets"></ul> </body> </html>