Javaã®HTTPã©ã¤ãã©ãªã§Twiter APIåå¾
HTTPéä¿¡ç¨ã®APIãHttpURLConnectionã使ã£ããµã³ãã«ã³ã¼ãã§ãã
Twitterã®public_timelineã¨ããAPIã¯ããã®åã®éããããªãã¯ã¿ã¤ã ã©ã¤ã³ï¼Twitterã®å
¨ã¦ã¼ã¶ã¼ã®å
¬éã¤ã¶ããä¸è¦§ï¼ãåå¾ããAPIã§ãããOAuthçã®èªè¨¼ãä¸è¦ãªããããµã³ãã«ã³ã¼ãã«ã¡ããã©è¯ãã§ãã
import static java.io.File.separator; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Twitter { // â»æ³¨ ããã¯Macç¨ã®ãã¹ãªã®ã§Windowsãªã"Desktop"ã"ãã¹ã¯ããã"ã«å¤ããã¨å¤åãã¾ããã public static final String DESKTOP = System.getProperty("user.home") + separator + "Desktop" + separator; public static final String URL_PUBLIC_TIMELINE = "http://api.twitter.com/1/statuses/public_timeline.xml"; public static void main(String[] args) throws IOException { Twitter twitter = new Twitter(); twitter.outputPublicTimelineXML(); } // public_timelineãXMLã§åºå public void outputPublicTimelineXML() throws IOException { URL url = new URL(URL_PUBLIC_TIMELINE); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); //connection.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3"); // æ¥ç¶ connection.connect(); //// ã¬ã¹ãã³ã¹ãããåºå //Map<String, List<String>> headerMap = connection.getHeaderFields(); //Set<String> keys = headerMap.keySet(); //for (String key : keys) { // List<String> list = headerMap.get(key); // System.out.println(key + ": " + list); //} // ã¹ãã¼ã¿ã¹ã³ã¼ã&ããã¹ããã¬ã¼ãºåºå int code = connection.getResponseCode(); String message = connection.getResponseMessage(); System.out.println(code + " " + message); // ãã¡ã¤ã«åºå outputFile(DESKTOP, connection.getInputStream()); // åæ connection.disconnect(); } // InputStreamãããã¡ã¤ã«åºå public void outputFile(String path, InputStream input) throws IOException { File file = new File(path + "public_timeline.xml"); BufferedReader br = new BufferedReader(new InputStreamReader(input)); BufferedWriter bw = new BufferedWriter(new FileWriter(file)); while (br.ready()) { String line = br.readLine(); bw.write(line); } if (br != null) br.close(); if (bw != null) bw.close(); } }
Twitterã¯ãXMLã¾ãã¯JSONå½¢å¼ã§ãªã½ã¼ã¹ãåå¾ã§ãã¾ãããã¨ã¯ãã¼ãµã¼èªåã§ä½ããã©ã¤ãã©ãªæ¢ãã¦ããããã¦ãæ¬å½ã«å¿
è¦ãªæååã ãåå¾ããã°Twitterã¯ã©ã¤ã¢ã³ããä½ãã¾ãããããä¸çªé¢åãªãã ãããã©ã
ã¡ãªã¿ã«ã"ã¬ã¹ãã³ã¹ããã"ã¨ã"ã¹ãã¼ã¿ã¹ã³ã¼ã"ã¨ã"ããã¹ããã¬ã¼ãº"ã¨ãã"Accept-Language"ã¨ãã®ç¨èªãã¿ã¦ãã³ãã³ã«ã³ãã³ãªäººã¯ãHTTPã®ä»æ§ãå°ãã ãå¦ã¶å¿
è¦ãããã¾ãããã¾ãªããWebãæ¯ããæè¡ãããã¹ãã§ããã
Webãæ¯ããæè¡ -HTTPãURIãHTMLãããã¦REST (WEB+DB PRESS plus)
- ä½è : å±±æ¬é½å¹³
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2010/04/08
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 143人 ã¯ãªãã¯: 4,320å
- ãã®ååãå«ãããã° (183件) ãè¦ã