public_streaming.SampleStream.java Source code

Java tutorial

Introduction

Here is the source code for public_streaming.SampleStream.java

Source

/*
 * Copyright 2007 Yusuke Yamamoto
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package public_streaming;

import twitter4j.Status;
import twitter4j.StatusAdapter;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

//
public class SampleStream {
    //OAUTH?
    final static String CONSUMER_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String CONSUMER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String ACCESS_TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    final static String ACCESS_TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    public static void main(String[] args) {
        // ?
        Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
                .setOAuthConsumerSecret(CONSUMER_SECRET).setOAuthAccessToken(ACCESS_TOKEN)
                .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET).build();
        // TwitterStream??
        TwitterStream twitterStream = new TwitterStreamFactory(configuration).getInstance();
        // Listener
        twitterStream.addListener(new Listener());
        // ????????????
        twitterStream.sample();
    }
}

/** Tweet????Listener */
class Listener extends StatusAdapter {
    // Tweet???????????
    public void onStatus(Status status) {
        System.out.println("tweet:" + status.getText());//
        System.out.println(status.getFavoriteCount());
        System.out.println("User" + status.getUser().getName() + "@" + status.getUser().getScreenName());//??,?(@xx)
        System.out.println("Posted Time:" + status.getCreatedAt());//
        System.out.println("Application Name:" + status.getSource());//
        System.out.println("Time Zone:" + status.getUser().getTimeZone());//
        System.out.println("Created Date:" + status.getUser().getCreatedAt());//?
        System.out.println("GeoLocation:" + status.getUser().getLocation());//?
        System.out.println("postID:" + status.getId());//ID
        System.out.println("UserID" + status.getUser().getId());//ID
        System.out.println("Language:" + status.getUser().getLang());//
        System.out.println("Follow:" + status.getUser().getFriendsCount());//
        System.out.println("Follower:" + status.getUser().getFollowersCount());//
    }
}