【问题标题】:Android: java.net.protocolException does not support outputAndroid:java.net.protocolException 不支持输出
【发布时间】:2012-11-06 09:14:39
【问题描述】:

我之前使用的是 HttpClientBasicNameValuePairs,由于某种原因我不得不改用 HttpUrlConnection

因此这段代码,使用某些参数发出 HttpPost 请求:

public class MConnections {

    static String BaseURL = "http://www.xxxxxxxxx.com";
    static String charset = "UTF-8";
    private static String result;
    private static StringBuilder sb;
    private static List<String> cookies = new ArrayList<String>();

    public static String PostData(String url, String sa[][]) {

        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) new URL(BaseURL + url)
                    .openConnection();
        } catch (MalformedURLException e1) {
        } catch (IOException e1) {
        }

        cookies = connection.getHeaderFields().get("Set-Cookie");
        try{
        connection.setDoOutput(true); // Triggers POST.
        connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=" + charset);
        }catch (Exception e) {

            //Here i get Exception that "java.lang.IllegalStateException: Already connected"
        }
        OutputStream output = null;
        String query = "";
        int n = sa.length;
        for (int i = 0; i < n; i++) {
            try {
                query = query + sa[i][0] + "="
                        + URLEncoder.encode(sa[i][1], "UTF-8");
            } catch (UnsupportedEncodingException e) {
            }
        }
        try {
            output = connection.getOutputStream();
            output.write(query.getBytes(charset));
        } catch (Exception e) {

            //Here i get Exception that "android: java.net.protocolException: Does not support output"
        } finally {

            if (output != null)
                try {
                    output.close();
                } catch (IOException e) {
                }
        }
        InputStream response = null;
        try {
            response = connection.getInputStream();
        } catch (IOException e) {

            //Here i get Exception that "java.io.IOException: BufferedInputStream is closed"
        } finally {

            //But i am closing it here
            connection.disconnect();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response, "iso-8859-1"), 8);
            sb = new StringBuilder();
            sb.append(reader.readLine());
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append("\n" + line);
            }
            response.close();
            result = sb.toString();
        } catch (Exception e) {
        }
        return result;
    }
}

但我得到了代码中注释的异常

实际上,我使用 AsyncTask 从我的 Activity 中调用了两次 MConnections.PostData()。这可能会导致 Exception: 已连接,但我正在使用 connection.disconnect。但是为什么我仍然得到那个异常?

我用错了吗?

谢谢

【问题讨论】:

    标签: android httpurlconnection


    【解决方案1】:

    对于协议异常,请在调用 getOutputStream() 之前尝试添加以下内容:

    connection.setDoOutput(true);
    

    感谢 Brian Roach 在这里的回答发现了这个答案:https://stackoverflow.com/a/14026377/387781

    旁注:我在运行 Gingerbread 的 HTC Thunderbolt 上遇到了这个问题,但在运行 Jelly Bean 的 Nexus 4 上却没有。

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多