- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
public class TwitterStreamImpl implements TwitterStream {
public void setUpStream() throws InterruptedException {
final String consumerKey = getTwitterCredentials().get(0).toString();
final String consumerSecret = getTwitterCredentials().get(1).toString();
final String token = getTwitterCredentials().get(2).toString();
final String secret = getTwitterCredentials().get(3).toString();
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
// add some track terms
endpoint.trackTerms(Lists.newArrayList("twitterapi", "#yolo", "trump", "donald", "lol"));
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Authentication auth = new BasicAuth(username, password);
// Create a new BasicClient. By default gzip is enabled.
BasicClient client = new ClientBuilder()
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Establish a connection
client.connect();
// Do whatever needs to be done with messages
for (int msgRead = 0; msgRead < 1000; 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 {
System.out.println(msg);
}
}
client.stop();
}
/**
* Reads twitterStup.txt from C:/Users/"user"/documents/ and returns them as
* an array
*
* @return Twitter Api Credentials
*/
private ArrayList getTwitterCredentials() {
BufferedReader in;
String str;
ArrayList<String> list = new ArrayList<String>();
try {
in = new BufferedReader(new FileReader("*******"));
while ((str = in.readLine()) != null) {
list.add(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
控制台日志显示:
Did not receive a message in 5 seconds
它每五秒就会说一次。我想“sysout”(实时)每条推文,其中包含端点 trackTerms 之一。但没有错误或类似的情况。代理可能有问题吗?
最佳答案
该代码正在运行,就像当时一样。问题出在代理上。因为我一直在办公室网络中,所以无法连接到流。所以我用自己的笔记本尝试了一下,你猜怎么着,它成功了。
关于java - 从 Twitter hbc API 获取推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843743/
我过去使用过 Twitter4j 来读取 twitter 公共(public)流,但没有使用过 twitter hbc (这是 twitter 的流媒体 api 的官方 java 库)。 我的用例是:
public class TwitterStreamImpl implements TwitterStream { public void setUpStream() throws Inter
我按照 HBC 中的快速入门进行操作我设法从 Twitter Stream 中获取了一些推文,其中指定了一些跟踪术语,代码如下: /** Declare the host you want to co
我正在开发一个使用 HBC 访问 Twitter 流 API 的项目。 我将 Twitter Streaming API(过滤器)的关键字存储在一个文件中,现在我正在寻找一种在每次文件更改时关闭并重新
我正在尝试使用 Twitter hbc library获取基于位置的推文。我已经根据示例文件 FilterStreamExample 设置了我的代码.该示例有效,但当我尝试向端点添加位置时,我收到 f
我需要根据某些主题标签过滤器读取推文,因此我使用 java hbc 客户端尝试了 Twitter 的流 api。首先,我尝试运行项目提供的示例代码,以了解它是如何工作的。 所以我: 在 Twitter
我打算在学校类里面使用 Twitter 进行一些语义文本分析。我下载了适用于 Java 的 Hosebird 客户端并正在运行 FilterStreamExample.java:https://git
我是一名优秀的程序员,十分优秀!