我需要根据某些主题标签过滤器读取推文,因此我使用 java hbc 客户端尝试了 Twitter 的流 api。首先,我尝试运行项目提供的示例代码,以了解它是如何工作的。
所以我:
- 在 Twitter 上声明了一个应用程序 + 生成了我的访问 key 、 token 和 secret
- 从 ( https://github.com/twitter/hbc ) 下载项目包
- 通过在 pom.xml 文件中填写 TODO 字段来配置示例项目 hbc-example 我的应用程序的 key 、 token 和 secret ( https://github.com/twitter/hbc/blob/master/hbc-example )
- 将示例项目 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() 中调用
- 由main调用
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/