← All articles
mobile

Android: Odd Error in DefaultHttpClient

DefaultHttpClient throwing Target host must not be null? Check your URI first, it is probably missing the http:// scheme prefix entirely.

Donn Felker

Donn Felker

2010.04.29 · 1 min read

I’m working on a couple of projects that interact with REST api’s and consume JSON data and in doing so I use the DefaultHttpClient from apache, located in the Android framework.

The Error

Last night I was working on a project and kept getting this error:

java.lang.IllegalStateException: Target host must not be null, or set in parameters.

I spent about 20 minutes banging my head against the wall, inspecting through the debugger, googling, etc. Long story short, there was not much info out there on how to fix this error. I found out what it was after another 20 minutes banging my head against the keyboard.

The Issue

I was connecting to website with the following URI:

www.example.org/api/json/get_stuff

The problem is that I was missing the “http://”. Therefore it should have been:

http://www.example.org/api/json/get_stuff

This fixed the issue. The reason is that the scheme was not provided in the URI. As soon as this was changed the entire client worked as expected.

Normally I wouldn’t blog this, but this is one of those issues where if I can’t find the resolution to the issue, I might as well blog it so others can find it when they run into it.

Donn Felker

Written by Donn Felker

I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.

Get the newsletter

Keep reading