Rajapandian
Rajapandian

Reputation: 9317

Getting java.net.UnknownHostException when using HostName in android

In my android application i have to get XML data from an URL(http://api.offersdb.com/distribution/beta/offers.json?api_key=demo&radius=10&postal_code=30305), for that i am using following code,

try {
        URL url = new URL(urlStr);                                  
        URLConnection urlConn = url.openConnection();
        if (!(urlConn instanceof HttpURLConnection)) {
            throw new IOException("URL is not an Http URL");
        }
        HttpURLConnection httpConn = (HttpURLConnection) urlConn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        resCode = httpConn.getResponseCode();
        if (resCode == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    }catch (Exception e) {
        e.printStackTrace();

    }

But it returns the following exception

java.net.UnknownHostException: Host is unresolved: api.offersdb.com:80

When i change the Host name (api.offersdb.com) with the ipaddress(XX.XX.XXX.XX) in the URL,i can get the response. I have no idea why i am getting this problem, please somebody help me.

Thanks in Advance,

Rajapandian

Upvotes: 1

Views: 1881

Answers (2)

Alexander
Alexander

Reputation: 61

Have you tried to set the following permission in the manifest file?

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

/ Alex

Upvotes: 1

Heiko Rupp
Heiko Rupp

Reputation: 30994

This means that name resolution is somehow broken on your device / the emulator. Are you able to use that URL from the built-in browser of your Android device?

Upvotes: 0

Related Questions