Example usage for twitter4j JSONObject toString

List of usage examples for twitter4j JSONObject toString

Introduction

In this page you can find the example usage for twitter4j JSONObject toString.

Prototype

public String toString(int indentSpaces) throws JSONException 

Source Link

Document

Encodes this object as a human readable JSON string for debugging, such as:
 { "query": "Pizza", "locations": [ 94043, 90210 ] }

Usage

From source file:com.dataspawn.twitterstreamer.Streamer.java

License:Apache License

public boolean run() {
    System.out.println("Thread is interrupted:" + Thread.currentThread().isInterrupted());
    Authentication auth;//from  w  w  w.j  a  va2s  .c  o  m

    BlockingQueue<String> queue;

    queue = new LinkedBlockingQueue<String>(10000);
    endpoint = new StatusesFilterEndpoint();

    endpoint.trackTerms(terms);

    //endpoint.locations(locations);

    //Authentication auth = new com.twitter.hbc.httpclient.auth.BasicAuth(username, password);
    auth = new OAuth1(consumerKey, consumerSecret, token, secret);

    client = new ClientBuilder().name("Streamer Client").hosts(Constants.STREAM_HOST).endpoint(endpoint)
            .authentication(auth).processor(new StringDelimitedProcessor(queue)).build();

    client.connect();
    try {
        ArrayList<String> tweets = new ArrayList<String>();
        Long msgRead = 0L;
        for (msgRead = 0L; msgRead < 1000L; msgRead++) {
            if (client.isDone()) {
                System.out.println(
                        "Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
                break;
            }

            String msg = queue.poll(5, TimeUnit.SECONDS);
            if (msg == null) {
                System.out.println("Did not receive a message in 5 seconds");
            } else {
                if (Thread.interrupted()) {
                    throw new InterruptedException();
                }
                try {
                    JSONObject tweet = new JSONObject(msg);
                    mStore.add(tweet.toString(4));
                    System.out.println(msg);
                } catch (IOException e) {
                    System.out.println("Tweet Store failed: " + e.getMessage());
                    return false;
                }

            }
        }

        System.out.println("Streamer read: " + msgRead);
        client.stop();

    } catch (Exception e) {
        System.out.print("Caught exception when reading tweets");
        // Print some stats
        System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
        return false;
    }
    // Print some stats
    System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
    return true;
}