java - 使用 Twitter-hbc 在 Java 中读取推文 - IOException

标签 java twitter httpclient twitter-oauth twitter-streaming-api

我需要根据某些主题标签过滤器读取推文,因此我使用 java hbc 客户端尝试了 Twitter 的流 api。首先,我尝试运行项目提供的示例代码,以了解它是如何工作的。

所以我:

  1. 在 Twitter 上声明了一个应用程序 + 生成了我的访问 key 、 token 和 secret
  2. 从 ( https://github.com/twitter/hbc ) 下载项目包
  3. 通过在 pom.xml 文件中填写 TODO 字段来配置示例项目 hbc-example 我的应用程序的 key 、 token 和 secret ( https://github.com/twitter/hbc/blob/master/hbc-example )
  4. 将示例项目 hbc-example 配置为在依赖项中同时使用 hbc-core 和 hbc-twitter4j 以避免依赖项错误

但每次我用 Maven 运行 hbc-example 时,我似乎总是得到:

(...)
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 Establishing a connection
[hosebird-client-io-thread-0] WARN com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 IOException caught when establishing connection to https://stream.t
witter.com/1.1/statuses/filter.json?delimited=length&stall_warnings=true
[hosebird-client-io-thread-0] WARN com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 failed to establish connection properly
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 Done processing, preparing to close connection
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 Establishing a connection
[hosebird-client-io-thread-0] WARN com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 IOException caught when establishing connection to https://stream.t
witter.com/1.1/statuses/filter.json?delimited=length&stall_warnings=true
[hosebird-client-io-thread-0] WARN com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 failed to establish connection properly
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 Done processing, preparing to close connection
[hosebird-client-io-thread-0] INFO com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 Establishing a connection
[hosebird-client-io-thread-0] WARN com.twitter.hbc.httpclient.ClientBase - hoseb
ird-client-0 IOException caught when establishing connection to https://stream.t
witter.com/1.1/statuses/filter.json?delimited=length&stall_warnings=true
(...)

当我使用 FilterStreamSample 类作为具有“main”函数的类运行 hbc-example 时,也会发生这种情况

查看 hbc-core 源代码,异常似乎是由 Connection 对象的 connect(HttpUriRequest request) 函数引发的(参见:hbc-master/src/main/java/com/twitter/hbc/httpclient/Connection)

关于 httpclient 异常的 Apache 文档没有提供太多关于如何处理此错误的线索,我想知道我是否在这里遗漏了一些东西,Twitter 的流 API 的更改阻止了我的代码运行,任何东西...

感谢您的帮助或建议!

Ps 1:仅供引用,connect(HttpUriRequest request) 是

  • 在 ClientBase.builtConnection(Connection conn, HttpUriRequest request) 中调用
  • 在 ClientBase.run() 中调用
  • 在 BasicClient.connect() 中调用
  • ma​​in调用

Ps 2:我的问题可能与这个尚未解决的问题类似:/questions/26363005/twitter-streaming-w-status-code-401

最佳答案

我给出的答案为时已晚,但可能对其他人有用。实际上,如果您的网络配置为与任何类型的代理一起使用,则此问题似乎是由于缺少代理主机和端口的配置而出现的。如果您可以浏览代码并设置网络的代理主机和端口,则此问题将得到解决。

P.S:我发现 Twitter Streaming api 非常简单且易于使用,但如果您使用一些配置了代理的网络(例如工作场所等),请不要忘记配置代理设置。

_configurationBuilder 是 twitter4j.conf.ConfigurationBuilder 的类型 _configurationBuilder.setHttpProxyHost("你的代理主机"); _configurationBuilder.setHttpProxyPort(您的代理端口号);

如果您错过了它,您将在收听消息时收到一些 twitterException,并且在谷歌搜索时也不会获得任何信息。

关于java - 使用 Twitter-hbc 在 Java 中读取推文 - IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569607/

相关文章:

java - 可用作编辑器 Pane 的组件

java - Spring @Transactional 持久方法不起作用

objective-c - ShareKit/Twitter 内存泄漏

java - 从 Java 应用程序创建推文

angular - 如何在 Angular 的 HttpClient 中使用 reportProgress?

java - 根据另一个数组中的特定条件将数组中的特定值相乘并将值存储在新数组中?

java - 如何检查 JDK 是为什么架构构建的?/在 mac 上安装的 JDK 是针对arm(又名arm64)还是针对Intel CPU(又名amd64)?

javascript - 如何打开可编辑的对话框来发布推文

c# https 调用与 crt 和 key 文件与密码

java - HttpClient 4.1 出现 SSL "Peer Not Authenticated"错误