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.
thanks for the info,i like your blog info because it's meant to me about learning something new..nice job!
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
No problem. I'm glad it helped you. π
Thanks to you, my head never even had to hit any walls. π
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).
Hmm – seems like an apache issue, but we'll see..
http://code.google.com/p/android/issues/detail?…
Awesome! Thanks for looking into that. I'm sure we'd all like to have cleaner error messages. Thanks!
Cool! Thanks for looking into it. I know all of us developers really
appreciate it.
Donn
Thanks!
THANKS!!
That was *exactly* my issue. I just wasted about 30 minutes trying to figure this one out b4 finding this website.
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.
Saved me AT LEAST 40 minutes. CHEERS.
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?
I'm glad it helped you. π
Thx a lot, save me a lot of time π
will take care of the error mentioned in the blog.
Thank you very I work for tow days on similar problem, and your blog just save me.
Thank you for this post.
I had the same problem!
dude thx just had this weird problem
Thank you.
Thankyou!
Thank you!
I had the same problem and you prevented my head form being banged on the keyboard for several minutes π
Thanks from me also! π
i love you
Thank you!
thanks!
you’re my hero.
Thank you you saved me a ton of time!
thanks a lot for your post it saved my time……
thank you
Thanks you saved my night π
You are my god buddy! I was wasting time for this a whole day…
thank you very much !!!!!!!!!!!!!!
Β 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.
Oh Duuuuuuddeeeeeee !!! you saved my french ass !! thx
Hey buddy this was really helpful to me…thanks a lot!!
Getting the same exception sometimes. Url is not encoded and it contains the prefix “http://” for both get and post requests.
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?