Android java.net.UnknownHostException: Host is unresolved

Android

Android Problem Overview


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.

I'm probably out of line asking this question again, a repeat of https://stackoverflow.com/questions/3293659/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.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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

Solution 5 - Android

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

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

Solution 6 - Android

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:

https://stackoverflow.com/questions/11277734/http-doesnt-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. "

Solution 7 - Android

You need to enable internet in your emulator.

  • Open emulator

  • Press menu button and open "settings"

  • Open "wireles and network"

  • Open "Mobile Networks"

  • Check on "Data enable"

Now test your application again.

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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.

Solution 11 - Android

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.

Solution 12 - Android

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

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

Solution 13 - Android

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 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.

Solution 14 - Android

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

Solution 15 - Android

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...

Solution 16 - Android

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.

Solution 17 - Android

Please try rebooting the emulator.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRuss BatemanView Question on Stackoverflow
Solution 1 - AndroidRuss BatemanView Answer on Stackoverflow
Solution 2 - AndroidyancynView Answer on Stackoverflow
Solution 3 - AndroidmilesologyView Answer on Stackoverflow
Solution 4 - AndroidbeetstraView Answer on Stackoverflow
Solution 5 - AndroidRahul ShirphuleView Answer on Stackoverflow
Solution 6 - AndroidJulienView Answer on Stackoverflow
Solution 7 - AndroidPratik PopatView Answer on Stackoverflow
Solution 8 - AndroidVito AndoliniView Answer on Stackoverflow
Solution 9 - AndroidDanView Answer on Stackoverflow
Solution 10 - AndroidhtafoyaView Answer on Stackoverflow
Solution 11 - AndroidAQ78View Answer on Stackoverflow
Solution 12 - AndroidLei CaoView Answer on Stackoverflow
Solution 13 - AndroidAntesh SharmaView Answer on Stackoverflow
Solution 14 - AndroidGouthamView Answer on Stackoverflow
Solution 15 - AndroidLalit JawaleView Answer on Stackoverflow
Solution 16 - AndroidsaswanbView Answer on Stackoverflow
Solution 17 - AndroidHieu - 7347514View Answer on Stackoverflow