我想我遇到了同样的 http://groups.google.com/group/android-developers/msg/9d37d64aad0ee357 结果这是Android 1.5的SDK。我碰巧打电话低于code(这是一种方法)有几次相同的URL,并间歇性失败。结果当它失败了,没有异常,流是空故readConnection失败,GETRESPONSE code返回-1。结果全球禁用缓存,setDefaultUseCaches(假);
我想一定有某种URL连接对象池的地方。
我如何能解决这个任何想法?
HttpURLConnection的连接= NULL; 尝试{ 网址URL =新的URL(this.url); 连接=(HttpURLConnection类)url.openConnection(); connection.setRequestProperty(授权,基本+ Base64编码coder.en codeString的(用户+:+密码)); connection.setRequestProperty(用户代理的userAgent); connection.connect(); readConnection(connection.getInputStream()); connection.disconnect(); }赶上(IOException异常前){ reportException(例如,connection.getResponse code()) }赶上(ParserException前){ reportException(例如,connection.getResponse code()) }
解决方案
我解决这个问题的方法是添加以下行...
System.setProperty(http.keepAlive,假);
...我的连接线之前...
连接=(HttpURLConnection类)url.openConnection();
祝你好运!
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())
}
解决方案
The way I fixed this issue was to add the below line...
System.setProperty("http.keepAlive", "false");
...before my connection line...
connection = (HttpURLConnection) url.openConnection();
Good luck!