Created
November 23, 2010 03:47
-
-
Save osima/711200 to your computer and use it in GitHub Desktop.
fetch my status of twitter using java
This file contains hidden or 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.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| import java.net.URL; | |
| import org.json.JSONArray; | |
| import org.json.JSONObject; | |
| public class GetMyStatus { | |
| static public void main(String[] a) throws Exception{ | |
| String user_id="tomoakioshima"; | |
| int count=3; | |
| String url="http://twitter.com/statuses/user_timeline/"+user_id+".json"+"?count="+count; | |
| //System.out.println( url ); | |
| StringBuffer sb=new StringBuffer(); | |
| try{ | |
| // | |
| // 1) statusを取得 | |
| // | |
| URL myurl=new URL(url); | |
| BufferedReader br = new BufferedReader( new InputStreamReader( myurl.openStream() ,"UTF-8") ); | |
| while(true){ | |
| String line = br.readLine(); | |
| if(line==null) | |
| break; | |
| sb.append(line); | |
| } | |
| br.close(); | |
| // | |
| // 2) 取得したstatusを JSONArray オブジェクトに変換 | |
| // | |
| String r=sb.toString(); | |
| JSONArray array = new JSONArray(r); | |
| for(int i=0; i<array.length(); i++){ | |
| Object obj=array.get(i); | |
| if( obj instanceof JSONObject ){ | |
| Object id = ((JSONObject)obj).get("id"); | |
| System.out.println(id); | |
| Object text = ((JSONObject)obj).get("text"); | |
| System.out.println(text); | |
| } | |
| } | |
| } | |
| catch(Exception ex){ | |
| ex.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment