Android: Odd Error in DefaultHttpClient

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.

Android Dev Digest

Get the best Android Developer posts delivered weekly to your inbox.

Don't worry, I wont spam (I hate that stuff too).

  • http://boom-boom-room.info/ boom boom room

    thanks for the info,i like your blog info because it's meant to me about learning something new..nice job!

  • Rob

    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

  • http://blog.donnfelker.com Donn Felker

    No problem. I'm glad it helped you. :)

  • Ben

    Thanks to you, my head never even had to hit any walls. :-)

  • Rgreenwalt

    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

    Hmm – seems like an apache issue, but we'll see..

  • Rgreenwalt
  • http://blog.donnfelker.com Donn Felker

    Awesome! Thanks for looking into that. I'm sure we'd all like to have cleaner error messages. Thanks!

  • http://blog.donnfelker.com Donn Felker

    Cool! Thanks for looking into it. I know all of us developers really
    appreciate it.

    Donn

  • Nello

    Thanks!

  • Meka103

    THANKS!!
    That was *exactly* my issue. I just wasted about 30 minutes trying to figure this one out b4 finding this website.

  • Raveesh Mca

    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

    Saved me AT LEAST 40 minutes. CHEERS.

  • http://blog.donnfelker.com Donn Felker

    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?

  • http://blog.donnfelker.com Donn Felker

    I'm glad it helped you. :)

  • Kemild0

    Thx a lot, save me a lot of time :)

  • http://www.talentsfromindia.com/hire-android-programmer-developer.html google android

    will take care of the error mentioned in the blog.

  • Assfpassal

    Thank you very I work for tow days on similar problem, and your blog just save me.

  • cupral

    Thank you for this post.
    I had the same problem!

  • Kasmo

    dude thx just had this weird problem

  • Zakhennahr

    Thank you.

  • http://www.conygre.com Nick Todd

    Thankyou!

  • Teots

    Thank you!

    I had the same problem and you prevented my head form being banged on the keyboard for several minutes :-)

  • http://shane.wikidot.com Shane Smith

    Thanks from me also! :)

  • love

    i love you

  • Serg

    Thank you!

  • Rob

    thanks!

  • That guy

    you’re my hero.

  • http://www.facebook.com/evan.damiano Evan Damiano

    Thank you you saved me a ton of time!

  • Dearirshad

    thanks a lot for your post it saved my time……

  • G Sampath004

    thank you

  • 6308635

    Thanks you saved my night :)

  • Armsky1988

    You are my god buddy! I was wasting time for this a whole day…

  • http://www.facebook.com/profile.php?id=100002390074038 Ghassen Khabacha

    thank you very much !!!!!!!!!!!!!!

  • Ravi

     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

    Oh Duuuuuuddeeeeeee !!! you saved my french ass !! thx

  • amj

    Hey buddy this was really helpful to me…thanks a lot!!

  • Shraddha

    Getting the same exception sometimes. Url is not encoded and it contains the prefix “http://” for both get and post requests.