本文介绍了android java.net.ProtocolException:已建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static Bitmap getImage(String address) throws Exception {

    Bitmap imgmap = null;
    InputStream is = null;

    URL url = new URL(address);

    HttpURLConnection  conn = (HttpURLConnection)url.openConnection();
    try {
        conn.setRequestMethod("GET"); >>> here was been excuted,but go to finally block
        conn.setConnectTimeout(5000);
        is = conn.getInputStream();
        byte[] imgbytes = StreamTool.getBytes(is);
        imgmap = BitmapFactory.decodeByteArray(imgbytes, 0, imgbytes.length);

    } finally {
        if (is != null) {
            is.close();
        }
        if (conn != null) {
            conn.disconnect();
        }
    }

    return imgmap;
}

之前conn.setRequestMethod(GET)被执行,Thorw异常Connection已经建立。谁可以给我一个解决方案

before conn.setRequestMethod("GET") was excuted,thorws the exception Connection already established. who can give me a solution

推荐答案

那是因为函数 setRequestMethod()必须在建立连接之前调用。
查看此链接

Thats because the function setRequestMethod() has to be called before the connection is made.Check this link

所以最好在openConnection()之前调用它。或者根本不打电话。

So better call it before openConnection(). Or dont call it at all.

这篇关于android java.net.ProtocolException:已建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 06:46