1
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;
}

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

Hariharan
  • 24,741
  • 6
  • 50
  • 54
acoder
  • 249
  • 5
  • 14

5 Answers5

7

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!

Vahid Ghadiri
  • 3,966
  • 7
  • 36
  • 45
  • I face the same question, will have problem if using debug mode, but do you know the reason behind? my link [http://stackoverflow.com/questions/34447190/cant-reset-method-already-connected?noredirect=1#comment56638318_34447190] – user1169587 Dec 24 '15 at 06:49
  • Unfortunately I have no idea about it. – Vahid Ghadiri Jan 02 '16 at 13:50
  • 1
    Exact same thing happened with me. What I did clean and rebuild the project, placed break points after output stream I opened for connection to write parameters. In specific at "InputStream inputStream=connection.getInputStream();" It worked like charm. I dont know why? how? It just works. Thanks guys – Sandeep Bhandari Jan 12 '16 at 21:48
  • Thank you. Break point was causing issue i spent couple of hour trying to fix it. – srbyk1990 Jan 22 '16 at 15:59
  • 1
    If anyone else is getting this in debug, check your watches. If you watch `getResponseCode()`, for example, your connection will implicitly connect. – Slotos Mar 01 '16 at 19:49
0

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.

Community
  • 1
  • 1
Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
0

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);
Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
Leo Paim
  • 500
  • 7
  • 9
0

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

-3

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.

Tejas
  • 585
  • 3
  • 15
  • 2
    This answer is complete nonsense. Before you call `openConnection()` you don't have an object you can call `setRequestMethod()` on. The link you provided doesn't state what you've claimed here. – user207421 Nov 15 '14 at 08:44