问题描述
你好我用下面的code建立一个URL连接。但随机我得到的回应code -1(这是响应code,其默认值):
Hi I'm using following code to establish a url connection. But randomly I get the responseCode -1 (which is the default value of responseCode):
try {
URL url = new URL(urlString);
HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
handleData(new DataInputStream(httpconn.getInputStream()), requestCode);
} else {
Log.e(TAG, "HttpConnection not OK: " + httpconn.getResponseCode());
ActivityHelper.httpError(this);
}
httpconn.disconnect();
} catch (Exception e) {
Log.e(TAG, "handleHttpConnection", e);
ActivityHelper.httpError(this);
}
我是不是做错了什么?因为它完全在9 10的估计尝试。
Am I doing something wrong? Because it works perfectly in estimated 9 of 10 attempts.
推荐答案
URLConnection的是越野车。
UrlConnection is buggy.
请参阅这的博客文章,从官方Android开发者博客为pre-姜饼的解决方法有一个问题。
See this blog post from the official Android Developer's blog for a pre-Gingerbread workaround for one problem.
我的建议是,不要使用它。它仍然是片状的,我在3.2。我切换到HttpClient的,事情已经不那么糟糕。
My advice, don't use it. It was still being flaky for me on 3.2. I switched to HttpClient and things have been less bad.
这篇关于HttpURLConnection的响应code是随机-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!