HttpURLConnection getResponseCode

3531 views
Skip to first unread message

Gil

unread,
Oct 16, 2008, 3:04:27 AM10/16/08
to Android Developers
I have a class that implements an HttpURLConnection. The class works
fine most of the time. Once in a while when I perform a GET operation
getResponseCode returns -1. Does anyone know under what circumstances
getResponseCode returns -1? The documentation states:

"the response code, -1 if no valid response code".

Ethereal shows that the GET request has been sent but that no bytes
have been received. Also, the getResponseCode does not time out. When
it fails it does so without waiting at all (typically <100ms).

androidian

unread,
Oct 16, 2008, 8:15:42 PM10/16/08
to Android Developers
I have the same problem. No idea what's happening.

Gil

unread,
Oct 16, 2008, 11:43:26 PM10/16/08
to Android Developers
I learned more about the problem. It always occurs after I cancel a
GET request. By cancel I mean quit the loop that reads from the input
stream, disconnect the HttpURLConnection and quit the thread which
runs the connection. Next HttpURLConnection I create exhibits the
problem described above. If I restart my application using Eclipse the
problem goes away. It also goes away some time after the canceled GET
request (simply wait a 30 seconds to a minute).

Michael Bleigh

unread,
Oct 27, 2008, 10:28:36 AM10/27/08
to Android Developers
Add me to the list of people who are experiencing this problem. Anyone
have solutions?

On Oct 16, 11:43 pm, Gil <virgildobjans...@gmail.com> wrote:
> I learned more about the problem. It always occurs after I cancel a
> GET request. By cancel I mean quit the loop that reads from the input
> stream, disconnect theHttpURLConnectionand quit the thread which
> runs the connection. NextHttpURLConnectionI create exhibits the
> problem described above. If I restart my application using Eclipse the
> problem goes away. It also goes away some time after the canceled GET
> request (simply wait a 30 seconds to a minute).
>
> On Oct 16, 5:15 pm, androidian <ianmcint...@gmail.com> wrote:
>
> > I have the same problem.  No idea what's happening.
>
> > On Oct 16, 4:04 pm, Gil <virgildobjans...@gmail.com> wrote:
>
> > > I have a class that implements anHttpURLConnection. The class works
> > > fine most of the time. Once in a while when I perform a GET operation
> > > getResponseCode returns -1. Does anyone know under what circumstances
> > > getResponseCode returns -1? The documentation states:
>
> > > "the response code, -1if no valid response code".

Michael Burton

unread,
Sep 17, 2009, 6:23:01 PM9/17/09
to Michael Bleigh, android-d...@googlegroups.com
Reviving an old thread here, but I found a solution that works for me
at least. Turned out to be a problem with http.keepAlive:

http://stackoverflow.com/questions/1440957/httpurlconnection-getresponsecode-returns-1-on-second-invocation

I don't know exactly why that's solving the problem, but the
workaround does work for me. Is this possibly a bug in Android?

Cheers,
Mike

Maps.Huge.Info (Maps API Guru)

unread,
Sep 17, 2009, 10:23:53 PM9/17/09
to Android Developers
I had this problem as well. I solved it by looping until I got a 200
response. It's 100% effective.

Here's is the relevant code:

http_response_code = -1 ;
HttpURLConnection conn = null ;
int iRetry = 0;
while ( iRetry < 10 ) {
iRetry++ ;
try {
conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setConnectTimeout (30000) ;
conn.setDoInput(true);
conn.connect();
http_response_code = conn.getResponseCode() ;
} catch (IOException e) {
e.printStackTrace();
http_response_code = -1 ;
}
// Status code HttpURLConnection.HTTP_OK == 200...
if (http_response_code == HttpURLConnection.HTTP_OK ) {
iRetry = 10 ;
}
}

-John Coryat

"What Zip Code?"

"Radar Now!"
Reply all
Reply to author
Forward
0 new messages