Skip to content

Instantly share code, notes, and snippets.

@osima
Created November 23, 2010 03:47
Show Gist options
  • Select an option

  • Save osima/711200 to your computer and use it in GitHub Desktop.

Select an option

Save osima/711200 to your computer and use it in GitHub Desktop.
fetch my status of twitter using java
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