【问题标题】:HttpURLConnection disconnect doesn't work in AndroidHttpURLConnection 断开连接在 Android 中不起作用
【发布时间】:2010-01-15 13:27:55
【问题描述】:

HttpURLConnection 中的 disconnect 方法似乎无法正常工作。 如果我执行以下代码:

url = new URL("http:// ...");
connection = (HttpURLConnection) url.openConnection ();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
// Some code
connection.disconnect();
connection.setDoInput(false); // -> IllegalStateException

当我调用setDoInput 方法时,我得到一个IllegalStateException。异常说:

已经连接

【问题讨论】:

    标签: android httpurlconnection illegalstateexception disconnect


    【解决方案1】:

    听起来您正在尝试重用连接?即在您与服务器断开连接后更改请求属性,准备建立另一个连接。

    如果是这样,那么只需创建一个新的HttpURLConnection 对象。

    【讨论】:

    • disconnect 方法的文档明确指出,以后不要尝试重用连接。
    • @Nate C-K 是的,我主张创建一个新的连接。也许您打算将此作为答案发布,或者作为对问题的评论??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多