package json;
import net.sf.json.*;
import java.io.*;
import java.net.*;
public class json_test {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String getS = getRequest();
JSONArray json = JSONArray.fromObject(getS);
for( int i=0; i<json.size(); i++ ) {
JSONObject timeline = (JSONObject)json.get(i);
//JSONArray
JSONObject user = timeline.getJSONObject("user");
System.out.println("No : " + (i+1) +
", Source : " +
timeline.get("source") +
", Retweeted : " +
timeline.get("retweeted") +
", Time_zone : " +
user.get("time_zone") +
", Retweet Count : " +
timeline.get("retweet_count"));
}
}
public static String getRequest() throws IOException {
String result = "";
String getRequest = "GET /1/statuses/public_timeline.json HTTP/1.1\r\n" +
"Host: api.twitter.com\r\n" +
"Connection: close\r\n" +
"\r\n";
InetAddress inetAddress = InetAddress.getByName("api.twitter.com");
Socket tcpSocket = new Socket(inetAddress.getHostAddress(), 80);
OutputStream os_socket = tcpSocket.getOutputStream();
InputStream is_socket = tcpSocket.getInputStream();
BufferedWriter bufferW = new BufferedWriter(new OutputStreamWriter(os_socket));
BufferedReader bufferR = new BufferedReader(new InputStreamReader(is_socket));
bufferW.write(getRequest);
bufferW.flush();
String str = null;
while( (str=bufferR.readLine()) != null ){
if( str.startsWith("[") ) result += str;
}
bufferW.close();
bufferR.close();
tcpSocket.close();
return result;
}
}