0

I am trying to download some images from some urls. The issue is that some of them are getting downloaded fine but some are giving error - java.net.UnknownHostException: Unable to resolve host "image-url": No address associated with hostname. Necessary permissions are already given, wifi is working fine too. Following is the exception:-

02-20 11:34:45.230: W/System.err(9336): java.net.UnknownHostException: Unable to resolve host "image-url": No address associated with hostname.
at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
02-20 11:34:45.230: W/System.err(9336):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-20 11:34:45.230: W/System.err(9336):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
02-20 11:34:45.240: W/System.err(9336):     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
02-20 11:34:45.240: W/System.err(9336):     at com.aquimo.downloadengine.ResourceDownloaderAndAllocator.loadFile(ResourceDownloaderAndAllocator.java:103)
02-20 11:34:45.240: W/System.err(9336):     at com.aquimo.downloadengine.ResourceDownloaderAndAllocator.access$0(ResourceDownloaderAndAllocator.java:72)
02-20 11:34:45.250: W/System.err(9336):     at com.aquimo.downloadengine.ResourceDownloaderAndAllocator$1.run(ResourceDownloaderAndAllocator.java:37)
02-20 11:34:45.250: W/System.err(9336):     at java.lang.Thread.run(Thread.java:841)
02-20 11:34:45.250: W/System.err(9336): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
02-20 11:34:45.250: W/System.err(9336):     at libcore.io.Posix.getaddrinfo(Native Method)
02-20 11:34:45.260: W/System.err(9336):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
02-20 11:34:45.260: W/System.err(9336):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)

Following is the code:-

HttpURLConnection ucon = (HttpURLConnection)url.openConnection();
ucon.setDoInput(true);
ucon.connect();
InputStream inputStream = ucon.getInputStream();
FileOutputStream fileOutput = new FileOutputStream(file);
Log.d(TAG, "inputStream size = " + inputStream.available());
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0)
{
    fileOutput.write(buffer, 0, bufferLength);
    downloadedSize += bufferLength;
    Log.i(TAG, "Progress: " + "downloadedSize:" + downloadedSize);
}
fileOutput.close();
Ethan Hunt
  • 366
  • 3
  • 17
  • `No address associated with hostname` what URL you are passing ? – Lucifer Feb 20 '14 at 06:42
  • http://www.fordesigner.com/imguploads/Image/cjbc/zcool/png20080526/1211810004.png – Ethan Hunt Feb 20 '14 at 06:43
  • are you trying in device which is behind the firewall ? – Lucifer Feb 20 '14 at 06:44
  • I am trying in my nexus4 and it does not has any firewall or antivirus. Moreover I can open this url in my browser. – Ethan Hunt Feb 20 '14 at 06:46
  • Seems to be some configuration with your dns. Are u getting the images from the internet or from a local server? – Kevin Joymungol Feb 20 '14 at 06:49
  • Try this link http://stackoverflow.com/questions/9475024/httpresponse-using-android-issue-execute-always-causes-exception – Hariharan Feb 20 '14 at 06:51
  • @KevinJoymungol I am getting them from internet. Also please note that some other urls are working fine and are returning the file. – Ethan Hunt Feb 20 '14 at 06:56
  • @HariharanTamilan No help from this link too. – Ethan Hunt Feb 20 '14 at 06:57
  • Please post the url, I can have a look. Also try your code on a data connection and see if it works. – Kevin Joymungol Feb 20 '14 at 06:59
  • @KevinJoymungol The url is posted above in the comments. http://www.fordesigner.com/imguploads/Image/cjbc/zcool/png20080526/1211810004.png. You can check this.. – Ethan Hunt Feb 20 '14 at 07:37
  • I have also had this issue on a Galaxy S3 (Sprint Android 4.3). I have experienced the exception being thrown from SipManager.Register(...) and from OnError raised from SipAudioCall.Listener when dialing. If I use the IP address instead of hostname, everything works fine. The failure is confined with the underlying API's ability to resolve the hostname. Oddly enough, manually resolving the hostname using InetAddress resolves successfully and then using the IP address in the URI works. The issue is only cleared when the phone is restarted and then works for a while before always erroring again. – Tim Apr 25 '14 at 19:40

1 Answers1

0
java.net.UnknownHostException: Unable to resolve host "image-url": No address associated with hostname

This means you are passing "image-url" as the URL to load. Unless you have this mapped to a valid IP, you're not going to get anything.

Synesso
  • 37,610
  • 35
  • 136
  • 207