android twitter4j oauth 在 3g 上不起作用
我使用 twitter4j 创建了一个 Android 应用程序。
在 3g 上时,所有 api 调用都无法进行身份验证,但在 wi-fi 上时却可以正常工作。 可能是什么问题?
I've created an android app using twitter4j.
None of the api calls can authenticate when on 3g, but work perfectly when on wi-fi.
What could be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此进行了广泛的测试,可以确认这只是 Android 4.0 (ICS) 设备上的问题。为什么,我还不确定,尽管 HttpURLConnection 的某些部分在 4.0 中发生了更改,因此其中一些更改使 twitter4j 不兼容。
我想可能是这样的:
Android 4.0 ICS 将 HttpURLConnection GET 请求转换为 POST 请求
但在使用 tcpdump 分析请求后,似乎它实际上正在进行有效的 GET,但由于某种原因,twitter api 仍然说这是一个坏的 。
除非有人能在 ICS/HttpURLConnection 中找到破坏此问题的内容,否则我能看到的唯一解决方法是为 twitter4j 编写一个使用 HttpClient 而不是 HttpURLConnection 的备用后端,这正是我打算做的
编辑:所以我为 twitter4j 编写了一个使用 apache httpclient 的客户端后端,它遇到了完全相同的问题!又过了一些不眠之夜,wireshark 给我带来了以下非常方便的解决方案,它确实有效:
像这样初始化你的 twitter4j 实例:
setUseSSL(true) 是一条神奇的线。
I've tested this extensively and can confirm this is only a problem on Android 4.0 (ICS) devices. Why, I'm unsure yet, though some parts of HttpURLConnection have changed in 4.0, so some of these changes have made twitter4j incompatible.
I thought it may have been this:
Android 4.0 ICS turning HttpURLConnection GET requests into POST requests
but after analysing the requests with tcpdump it seems like it is in fact making a valid GET, but for some reason, the twitter api still says it's a bad request..
Unless someone can find what it is in ICS/HttpURLConnection that is breaking this the only workaround I can see is to write an alternate backend for twitter4j that uses HttpClient instead of HttpURLConnection, which is exactly what I plan to do.
EDIT: So I wrote the a client backend for twitter4j that uses the apache httpclient and it suffers from the exact same problem!! Some more sleepless hours and wireshark has brought me to the following very handy solution, that actually works:
Initialise your twitter4j instance like this:
The setUseSSL(true) is the magic line.