Twitter streaming api is not work

hbc
java

#1
public class FilterStreamExample {
    public static void main(String[] args) throws InterruptedException {
        run("consumerKey", "consumerSecret", "token", "secret");
    }

    public static void run(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
        endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo"));
        Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
        Client client = new ClientBuilder()
                .hosts(Constants.STREAM_HOST)
                .endpoint(endpoint)
                .authentication(auth)
                .processor(new StringDelimitedProcessor(queue))
                .build();
        client.connect();
        for (int msgRead = 0; msgRead < 1000; msgRead++) {
            String msg = queue.take();
            System.out.println(msg);
        }
        client.stop();
    }
}

i don’t know why get nothing response


#2

Which client library are you using - is this Twitter4J?

Is there any output at all from this code, or does it just sit and do nothing indefinitely?


#3

Ah this looks like hbc. I’ll see if I’m able to reproduce.


#4

I’ve just successfully run this by checking out hbc from Github; modifying the hbc-example pom.xml to set the FilterStreamExample as the mainClass; uncommenting the main() method in FilterStreamExample.java; and running via mvn exec.

I’ve tried your version of the code as well and it works here.

Note that what this code is doing is filtering the stream for Tweets containing #yolo or twitterapi - those probably don’t happen very often, so I had to Tweet the hashtag from my test account to trigger a Tweet to arrive in the code.

You should see output similar to this:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Hosebird Client Examples 2.2.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ hbc-example >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ hbc-example <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ hbc-example ---
[com.twitter.hbc.example.FilterStreamExample.main()] INFO com.twitter.hbc.httpclient.BasicClient - New connection executed: hosebird-client-0, endpoint: /1.1/statuses/filter.json?delimited=length&stall_warnings=true
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hosebird-client-0 Establishing a connection
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hosebird-client-0 Processing connection data
{"created_at":"Wed Mar 22 17:29:43 +0000 2017","id":844601998863482884,"id_str":"844601998863482884","text":"testing #yolo","source":"\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":54931584,"id_str":"54931584","name":"Evil Piper","screen_name":"evilpiper","location":null,"url":null,"description":"mwah-ha-haaaaa (aka, squirrel!)","protected":false,"verified":false,"followers_count":28,"friends_count":17,"listed_count":1,"favourites_count":30,"statuses_count":597,"created_at":"Wed Jul 08 15:40:42 +0000 2009","utc_offset":0,"time_zone":"London","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"1A1B1F","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"91061F","profile_sidebar_border_color":"181A1E","profile_sidebar_fill_color":"949399","profile_text_color":"120312","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/307611594\/evil_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/307611594\/evil_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/54931584\/1400069736","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"yolo","indices":[8,13]}],"urls":[],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"filter_level":"low","lang":"en","timestamp_ms":"1490203783132"}

#5

When i try this code, it just blocked and get nothing. Maybe network led to this situation.I get restApi through Twitter4J through shadowSocks(without this get nothing). But this not work. If it does, what should i do to use streaming api?


#6

I’m not familiar with shadowSocks.

Does the code not even print the information message on connection i.e. INFO com.twitter.hbc.httpclient.BasicClient - New connection executed: hosebird-client-0, endpoint: /1.1/statuses/filter.json?delimited=length&stall_warnings=true

Are you able to connect to api.twitter.com from your location at all?


#7

like this below


when i connect to api.twitter.com, i get this.


#8

Hi,

I think my reply would be very late compared to this following question posted. Even I was having the same issue, I thoroughly checked my code but the issue wasnt with code. On the contrary the issue was with the Machine time(CPU time) with internet time. My machines time was 5 minutes less than compared to the present internet time so thats why I was getting the same problem.

Suppose if you are running your code in Ubuntu OS the use the following command

: sudo ntpdate time.nist.gov

to make your machine’s time equal to internet time and then try to run your code for Streaming API.

You wont be having this issue if you are using RESTful API of Twitter(using twitter4j)