HttpURLConnection的响应code是随机-1 [英] HttpURLConnection responsecode is randomly -1

查看:297
本文介绍了HttpURLConnection的响应code是随机-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我用下面的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆