Created
December 6, 2011 06:56
-
-
Save matsumotius/1437119 to your computer and use it in GitHub Desktop.
twitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
import java.util.*; | |
import javax.xml.parsers.*; | |
import org.xml.sax.*; | |
import org.xml.sax.helpers.*; | |
import java.io.*; | |
public class Twitter extends DefaultHandler { | |
public Boolean is_tweet = false; | |
public static void main(String[] args) | |
throws MalformedURLException, ProtocolException, IOException, ParserConfigurationException { | |
URL url = new URL("http://api.twitter.com/1/statuses/user_timeline.xml?id=eve"); | |
HttpURLConnection urlconn = (HttpURLConnection)url.openConnection(); | |
urlconn.setRequestMethod("GET"); | |
urlconn.setInstanceFollowRedirects(false); | |
urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); | |
urlconn.connect(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream())); | |
try { | |
SAXParserFactory spfactory = SAXParserFactory.newInstance(); | |
SAXParser parser = spfactory.newSAXParser(); | |
parser.parse(urlconn.getInputStream(), new Twitter()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
reader.close(); | |
urlconn.disconnect(); | |
} | |
public void startDocument() { | |
System.out.println("start timeline"); | |
} | |
public void startElement(String uri, String localName, String qName, Attributes attributes) { | |
if(qName == "text") this.is_tweet = true; | |
} | |
public void characters(char[] ch, int offset, int length) { | |
if(this.is_tweet) System.out.println("tweet:" + new String(ch, offset, length)); | |
} | |
public void endElement(String uri, String localName, String qName) { | |
if(qName == "text") this.is_tweet = false; | |
} | |
public void endDocument() { | |
System.out.println("end timeline"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment