Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android java.net.UnknownHostException: Host is unresolved

Tags:

android

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

like image 734
Russ Bateman Avatar asked Aug 18 '10 16:08

Russ Bateman


People also ask

What is Java net UnknownHostException?

The UnknownHostException occurs when trying to connect to a remote host using its hostname, but the IP address of the host could not be determined. This usually happens because of a typo in the hostname, or because of a DNS misconfiguration or propagation delay.


2 Answers

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

like image 64
Russ Bateman Avatar answered Oct 19 '22 02:10

Russ Bateman


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
like image 33
yancyn Avatar answered Oct 19 '22 03:10

yancyn