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.
boom boom room says
thanks for the info,i like your blog info because it's meant to me about learning something new..nice job!
Rob says
THANK you! Exactly what I hit, and since I was previously stung by HttpClient not wanting the http:// at the start of the URL, the last thing I would've tried was adding it back on for this. Thanks again for bothering to post π
Rob
Donn Felker says
No problem. I'm glad it helped you. π
Ben says
Thanks to you, my head never even had to hit any walls. π
Rgreenwalt says
I work on android for google and hit this issue too – absurd error message.. I'll file a bug report and hopefully this gets fixed (either a better error message or the underlying problem).
Rgreenwalt says
Hmm – seems like an apache issue, but we'll see..
Rgreenwalt says
http://code.google.com/p/android/issues/detail?…
Donn Felker says
Awesome! Thanks for looking into that. I'm sure we'd all like to have cleaner error messages. Thanks!
Donn Felker says
Cool! Thanks for looking into it. I know all of us developers really
appreciate it.
Donn
Nello says
Thanks!
Meka103 says
THANKS!!
That was *exactly* my issue. I just wasted about 30 minutes trying to figure this one out b4 finding this website.
Raveesh Mca says
08-18 10:43:07.293: ERROR/AndroidRuntime(209): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
Im passing the URL as string as follows :
new HttpGet(URLEncoder.encode(“http://chart.apis.google.com/chart?cht=p&chd=e:5mGa&chs=500×200&chts=000000,16&chtt=2D+Pie+Chart&chco=FFFF00,FF0000&chl=Ms.+Pac-Man|Red+Lips.png”));
But getting the above error? Any idea how to fix it? Please help me. The url is working perfectly in Firefox browser. But getting error in device.
Abaratha says
Saved me AT LEAST 40 minutes. CHEERS.
Donn Felker says
Raveesh – In the debugger, what is the URI that is output from the HttpGet? Looking at that may give you some insight. For example, are some of the characters being encoded that should not be?
Donn Felker says
I'm glad it helped you. π
Kemild0 says
Thx a lot, save me a lot of time π
google android says
will take care of the error mentioned in the blog.
Assfpassal says
Thank you very I work for tow days on similar problem, and your blog just save me.
cupral says
Thank you for this post.
I had the same problem!
Kasmo says
dude thx just had this weird problem
Zakhennahr says
Thank you.
Nick Todd says
Thankyou!
Teots says
Thank you!
I had the same problem and you prevented my head form being banged on the keyboard for several minutes π
Shane Smith says
Thanks from me also! π
love says
i love you
Serg says
Thank you!
Rob says
thanks!
That guy says
you’re my hero.
Evan Damiano says
Thank you you saved me a ton of time!
Dearirshad says
thanks a lot for your post it saved my time……
G Sampath004 says
thank you
6308635 says
Thanks you saved my night π
Armsky1988 says
You are my god buddy! I was wasting time for this a whole day…
Ghassen Khabacha says
thank you very much !!!!!!!!!!!!!!
Ravi says
Β http://staging_api.visitorm.com/v1/63814fc0e12c012e2f7312313b036653/apps/4.json
same error is here I found a reason “_” in my url I try diffrnt diffrnt encoding systm in android but not success any one can help meΒ
java.lang.IllegalStateException:host must not be null.
julien says
Oh Duuuuuuddeeeeeee !!! you saved my french ass !! thx
amj says
Hey buddy this was really helpful to me…thanks a lot!!
Shraddha says
Getting the same exception sometimes. Url is not encoded and it contains the prefix “http://” for both get and post requests.
Suparna Mukherjee says
Using a wamp server. TRying to access a xml file from local host
Url is 10.0.2.2/suparna/abc.xml
but it’s not working. Can you help?