HttpsURLConnection 间歇性地失败到同一 URL
我想我正在经历同样的事情 http://groups.google.com/group/android-developers/msg/ 9d37d64aad0ee357
这是Android 1.5 SDK。 我碰巧在下面的代码(在一个方法中)中调用了几次 相同的网址,但它间歇性失败。
当失败时,也不例外,流是空的,所以 readConnection 失败,getResponseCode 返回 -1。
禁用全局缓存,setDefaultUseCaches(false);
我想某处一定有某种 url 连接对象池。
知道如何解决这个问题吗?
HttpURLConnection connection = null;
try {
URL url = new URL(this.url);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "basic " +
Base64Coder.encodeString(user + ":" + password));
connection.setRequestProperty("User-Agent", userAgent);
connection.connect();
readConnection(connection.getInputStream());
connection.disconnect();
} catch (IOException ex) {
reportException(ex, connection.getResponseCode())
} catch (ParserException ex) {
reportException(ex, connection.getResponseCode())
}
I think I'm experiencing the same as
http://groups.google.com/group/android-developers/msg/9d37d64aad0ee357
This is Android 1.5 SDK.
I happen to call several times below code(which is in a method) with
the same url and it fails intermittently.
When it fails, there is no exception, the stream is empty so the
readConnection fails, and getResponseCode returns -1.
Global caching is disabled, setDefaultUseCaches(false);
I suppose there must be some kind of url connection object pool somewhere.
Any idea on how can I workaround this?
HttpURLConnection connection = null;
try {
URL url = new URL(this.url);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "basic " +
Base64Coder.encodeString(user + ":" + password));
connection.setRequestProperty("User-Agent", userAgent);
connection.connect();
readConnection(connection.getInputStream());
connection.disconnect();
} catch (IOException ex) {
reportException(ex, connection.getResponseCode())
} catch (ParserException ex) {
reportException(ex, connection.getResponseCode())
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决这个问题的方法是添加以下行...
...在我的连接线之前...
祝你好运!
The way I fixed this issue was to add the below line...
...before my connection line...
Good luck!
如果 getResponseCode 返回 -1,则意味着存在某种连接错误。
if getResponseCode returns -1 it means that there was a connection error of some sort.