“-1 error loading URL” on Android

This beautifully descriptive error was thrown back to me last night on every second http connection that I made. It was happening both through JSoup and the regular Android httpclient.  After much fruitless digging around it became apparent that there was something going wrong in the connection pool when it came to handling cookies.

To cut a long story short there’s a bug in the Android tracker about this issue and it is fixed in Android 2.2 (Froyo) and up.  To solve the issue on a Eclair or lower you will need to do something like this, which will disable keep-alive throughout your entire app.  You can also control this on a connection level if you use connections in multiple places.

// Work-around for pre-froyo
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
    System.setProperty("http.keepAlive", "false");
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s