android java.net.ProtocolException: Connection already established

后端 未结 5 2019
南笙
南笙 2020-12-20 06:27
public static Bitmap getImage(String address) throws Exception {

    Bitmap imgmap = null;
    InputStream is = null;

    URL url = new URL(address);

    HttpURLC         


        
相关标签:
5条回答
  • 2020-12-20 06:49

    When you create an instance of HttpURLConnection it defaults to the request method GET so there is no need to call setRequestMethod in this instance.

    This link contains some fantastic detail about HTTP connections and how to use them.

    0 讨论(0)
  • 2020-12-20 07:04

    Experienced same, find out the IDE is watching a field of the connection which I have added to debug, then cause the connection get established before other code run like set properties.

    So if you want to get some "properties" before connection is ready during debug, you will get this error

    0 讨论(0)
  • 2020-12-20 07:07

    This is happening because you're connecting before setting the request method

    A suggest you try this:

    conn.setRequestMethod("GET");
    conn.connect);
    conn.setConnectTimeout(5000); 
    is = conn.getInputStream();
    byte[] imgbytes = StreamTool.getBytes(is);
    
    0 讨论(0)
  • 2020-12-20 07:11

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

    http://developer.android.com/reference/java/net/HttpURLConnection.html#setRequestMethod(java.lang.String)

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

    0 讨论(0)
  • 2020-12-20 07:13

    I know it is completely nonsense. But it happens for me in debug mode when I put break point on this line.

    con.setRequestMethod()

    As soon as I removed the break point, the error has gone!

    0 讨论(0)
提交回复
热议问题