Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android:java.net.protocolException不支持输出大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_772_0@
我以前使用httpClient和BasicNameValuePairs,由于某种原因我必须转移到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中@L_956_3@mConnections.PostData()两次.这可能会导致异常:已连接,但我正在使用COnnection.disconnect.但为什么我还会得到这个异常?

我用错了吗?

谢谢

解决方法

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

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

旁注:我在运行Gingerbread的HTC Thunderbolt上出现了这个问题,但不是在运行jelly Bean的Nexus 4上.

大佬总结

以上是大佬教程为你收集整理的Android:java.net.protocolException不支持输出全部内容,希望文章能够帮你解决Android:java.net.protocolException不支持输出所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。