Arutha
Arutha

Reputation: 26488

HttpURLConnection disconnect doesn't work in Android

The method disconnect from HttpURLConnection seems not to work properly. If I execute the following code:

url = new URL("http:// ...");
connection = (HttpURLConnection) url.openConnection ();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
// Some code
connection.disconnect();
connection.setDoInput(false); // -> IllegalStateException

I get an IllegalStateException when I call the method setDoInput. The exception says:

Already connected

Upvotes: 1

Views: 2757

Answers (1)

Christopher Orr
Christopher Orr

Reputation: 111565

It sounds like you're trying to reuse the connection? i.e. altering the request properties after you've disconnected from the server, ready to make another connection.

If that is the case, then just create a new HttpURLConnection object.

Upvotes: 1

Related Questions