Android java.net.UnknownHostException: Host is unresolved

This code doesn’t work:

URL         url   = new URL( xmlPath );
InputSource input = new InputSource( url.openStream() );

all the time, resulting in an UnknownHostException, even when the host exists, has been hit successfully using the same code if from a different development host (machine), and also from a browser.

  • Detect when a fragment is drawn?
  • Using new IMMERSIVE mode in android kitkat
  • Activity transition not performed (Lollipop)
  • How can I put utf-16 characters in Android string resource?
  • How to set foreground attribute to other non FrameLayout view
  • Google documents mime type
  • I’m probably out of line asking this question again, a repeat of Android java.net.UnknownHostException: Host is unresolved (strategy question). The forum software would not allow me to comment on the unsatisfactory answer to that question (in order to solicit more attention to the solution I ultimately found). Perhaps I’m not popular enough yet to be given that privilege. In the meantime, I’m essentially re-posting the question and a different answer.

    Basically, I accept that maybe the Android device has a hard time with DNS under certain circumstances, but I’ve tried, for example the anddev.org example of how to use the SAX parser, and it worked on one machine I have and not on another.

    Edit:
    As noted, I know a working answer and will post it.

    I am a) asking a question (that’s already been asked and unsuccessfully answered) and b) answering it for the benefit of those for whom my answer will be useful.

    Related posts:

    java.lang.IllegalArgumentException: No view found for id 0x1020002 (android:id/content) for fragment
    NotificationManager getActiveNotifications() for older devices
    Including a QR-scanner application with PhoneGap
    How to load fragment data only when its tab is clicked in PagerSlidingTabStrip
    Highlight for selected item in expandable list
    How to handle a lot of different view types in recyclerview viewholder
  • How to get uid value of an android application from a list displayed in a spinner?
  • How to build my own Android SDK
  • Is it possible to edit Androidmanifest.XML After packaging the APK?
  • Android - MediaPlayer's on Prepare Called even before the stream is prepared on Android 4.0+
  • Can you repeatedly deploy an APK with 'adb install' without uninstalling first?
  • weird exceptions under android 4.0 with eclipse
  • 16 Solutions collect form web for “Android java.net.UnknownHostException: Host is unresolved”

    The answer is devilishly simple: remove, then re-create your AVD (virtual device/emulator) in Eclipse. It worked for me–first time.

    I been follow the steps above but still try no luck, finally i just realize my app did not allow to go outside internet in the emulator. So just add users permissions in your manifest file. Done! Thanks to jozsi see http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

    1. android.permission.INTERNET
    2. android.permission.ACCESS_NETWORK_STATE
    3. android.permission.READ_PHONE_STATE

    I have seen this on a phone as well (while doing USB debugging). The solution was to disable WiFi, and re-enable it.

    In my case I was downloading an XML file from a URL on my phone but was having the issue describe in this thread.

    for fixing it, I not only needed to have the regular internet permission

    <uses-permission android:name="android.permission.INTERNET" /> 
    

    But I also needed

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    As a suggestion to see if your device may need the same thing, try navigating to the URL in your phone’s browser to see if prompts about needing SD Card or something.

    Hope that helps others.

    It work’s for me by adding user’s permission for Internet in AndroidMenifest.xml file

    <uses-permission android:name="android.permission.INTERNET"/>
    

    Solution is here
    You need to enable internet in your emulator nothing else.

    • open emulator

    • press menu button and open “settings”.

    • open “wireles and network”.

    • open “Mobile Networks”.

    • check on “Data enable”

    Done!!
    Now , test your application again.

    Cheers!

    In my case the problem was elsewhere. I had to:

    • add <uses-permission android:name="android.permission.INTERNET" />

    • setup a proxy server on my emulator : Emulator > settings > wireless and network > Mobile Networks > Access Point Name > "the access point you have ;)". Here you can set all the proxy port url and login/pass you want.

    AND

    • Call my network operation in a separate thread. The easy way was to implement an AsynchTask.

    I found the solution here:

    HTTP doesn't work in Android emulator

    ” This exception is there for a reason. Network activity can take time,
    performing networking on the main thread, which is the same thread
    that responsible for updating the UI, would freeze the thread until
    the networking is done (this is what happens on every thread, but when
    it’s performed on a dedicated thread, it’s ok). In Android, if the
    UI-thread isn’t active for 5 seconds, it will show the Application is
    not responsive, Do you want to close it? dialog. “

    I couldn’t get rid of the emulator “UnknownHostExceptions” – and the emulator’s browser wouldn’t get a connection either. Tried all suggestions I could find at stack overflow and everywhere else.

    Finally I deleted my AVD, shut down Eclipse, and then created the AVD via the command line. Then launched it via command line with the -dns-server 8.8.8.8. (on my Mac: ./emulator -avd Google-8 -dns-server 8.8.8.8) It finally worked!

    Hope this helps someone else.

    Thank you so much Russ. I’m glad you persisted. I had spent the best part of an hour reading all kinds of possible fixes to this error. Just for anyone else stumbling across this thread, i’ll briefly recap here:
    -Check Internet uses permission is in the manifest
    -Catching the exception (pointless)
    -Changing firewall settings on your OS
    -Share connection (windows only)
    And a load of other stuff

    My fix took a little more than yours, but not much.
    After reading your post, I closed and destroyed all emulators. Then I noticed that when I moused over the run button (green circle with white arrow) it said the program was already running, even though no AVDs (emulators) existed! Anyway, restarted eclipse, created a AVD and ran it and Bob was my father’s brother. Nothing to do with my code or setup at all.

    I found this afterwards. It may well achieve the same thing but I can’t vouch for it unless the problem reoccurs.
    http://www.jiahaoliuliu.com/2011/02/stopping-android-applications-already.html
    Another trick that /may/ achieve the same result is tick “wipe user data” in Run Configurations, Target tab.

    My problem was when sharing the internet from the computer via WIFI, after ethernet connection was lost and reconnected I couldn’t achieve connection on the phone. This was fixed by restarting internet sharing on computer.

    Hope it helps someone.

    In my case I was using host name based URL with HttpURLConnection. However, using an IP based url, resolved the issue. I also deleted/recreated the AVD but that didn’t help.

    I just restart the AVD and add the User Permission then it works.

    <uses-permission android:name="android.permission.INTERNET" />

    issue resolved

    run cd C:\Program Files\Android\android-sdk\tools

    run android list avd in cmd returns list of ur availabe avd…. in my case its Myabc_avd

    run emulator @Myabc_avd -dns-server 8.8.8.8 in cmd …avd will show avd
    go to avd’s browser and type http://www.google.com will show google page.
    u can now access browser using hostname like myfrstapp.com

    if all go well then u can run ur project using eclipse but make sure it is running in avd which is running from cmd.

    In my case i have just restarted the Emulator and it worked.

    It’s so simple. No need of Restart eclipse OR delete and creating Emulator, you just need to follow the steps given below:

    1. Right Click on your project
    2. Click “Run as
    3. Click “Run configuration
    4. Window will get opened
    5. In left panel select your project run configuration
    6. Then in right panel open “Target” tab
    7. Scroll down
    8. You will get an option at bottom called “Additional Emulator Command Line Arguments
    9. Put “-dns-server 8.8.8.8” these values (without double quotes) in text box.

    That’s it…

    I encountered this problem when I had started the emulator while I was logged on to a VPN. Logging off the VPN had no effect until I quit the emulator and restarted. It is easy to test if your emulator is not resolving names correctly – just go to the emulator browser and type in a website hostname.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.