Skip to Content
Author's profile photo Former Member

Search Twitter from Java

Twitter is the social network used very widely for promoting products, films, etc. It is loved by companies especially for marketing. Twitter API owes the biggest part in Twitter’s success. The API is providing you the ability to do every possible operation that you can do in the website through your programs. For example, you find twitter tweets in many other sites (even in this blog) and this is achieved using Twitter API. Using this API you can do a lot of innovative things similar to the viral marketing done by Warner Bros for The Dart Knight Rises.

Here I would like to give you an intro to the Twitter Search API. It is simplest of all Twitter API. It doesn’t need any authentication. If you want to search all the recent tweets that contain the word ‘darkknight’, you can do it using your own program and print the results! Here I’m using Java to implement it. Now we will see about Twitter Search API.

This is the URL you are going to access to search the tweets. You have to pass a query string and the server will return you the result tweets in JSON format. Then you have to parse the returned JSON data and produce the result.

For example,

The above URL will return the tweets containing the word ‘darkknight’. Just enter the URL in your browser and see the result in view source. Similar to q there are many other query parameters available. You can get the detailed documentation of the API at

To parse the JSON data, we need a JSON parser. I used the following library for this purpose

import org.json.JSONArray;
import org.json.JSONObject;
 * @author Fareez Ahamed
 * @email
public class Main {
     * @param args the command line arguments
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(
                new InputStreamReader(;
        String urlstr = "";
        StringBuffer buff = new StringBuffer();
        System.out.print("Search for : ");
        urlstr += in.readLine();
        URL url = new URL(urlstr);
        BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
        int c;
        JSONObject js = new JSONObject(buff.toString());
        JSONArray tweets = js.getJSONArray("results");
        JSONObject tweet;
        for(int i=0;i<tweets.length();i++) {
            tweet = tweets.getJSONObject(i);
            System.out.println((i+1)+") "+tweet.getString("from_user")+" at "+tweet.getString("created_at"));

The above program gets a string and displays the recent tweets which contain the string along with the username and tweeted time.

Now, in the Twitter Search API documentation, carefully go through the sample JSON data shown to understand which fields are used for what purpose. The response from the Search API is always a single JSON object. Every response JSON object contains a field named ‘results’ which is the array of tweets. Each object in the array represents a single tweet and contains all the information regarding the tweet. In the above program, I access only three fields for a simple implementation. They are, from_user which gives the username of the user who tweeted, created_at which gives the time and text which is the tweet text.


Search for : darkknight
1) _malam at Sat, 05 May 2012 14:52:23 +0000
@__darkknight hi

2) Mauxa at Sat, 05 May 2012 14:48:13 +0000
@DarkKnight_IT Batman - The Dark Knight Rises, record di visite per il trailer inglese

3) Mauxa at Sat, 05 May 2012 14:47:40 +0000
@DarkKnight_IT Batman - The Dark Knight Rises, Anne Hathaway è un’ambigua Catwoman

4) mike_mc07 at Sat, 05 May 2012 14:47:03 +0000
#TheAvengers is the most EPIC movie since #DarkKnight

5) JulieHicks21 at Sat, 05 May 2012 14:46:57 +0000
Every time I see @mvp86hinesward running the ball on the #darkknight preview I kind of wanna cry :sad:

Hope you would love to try it.

Fareez Ahamed


Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Janarthanan Elangovan
      Janarthanan Elangovan

      Hi Fareez,

      Awesome blog about twitter search API. at-last i learnt something about how s twitter functions r working 😉   etc., Thank u fareez for sharing and congrats..!!  🙂