java.net.UnknownHostException: Unable to resolve host “<url here>”; No address associated with hostname

Jawad picture Jawad · Nov 18, 2016 · Viewed 12.6k times · Source

I am using AsychTask function to call URL "http://xx2/postdata/DATA2.asp?count=1&CIHAZID=53&ISLEMZAMANI=18102016190815&GZBHID=28198" of localhost server. While debugging on Android Device with proper and working Ethernet connection, instead of getting "OK" response its producing UnKnownHostException

I have tried all the solutions mentioned on different sites and pages, such as "add internet and network access permissions in manifest file", or "change the targetSdkVersion to 22" and many more..

The internet permission is already set in the manifest file.

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

If I call the URL in browser of the android device it gets "OK" as a response, but my application it does not. Following is AsyncTask Function I am using to call the URL

// AsyncTask Function to send TempReports Table records to server
private class sendLogsToServer extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {

        URLConnection feedUrl;
        StringBuilder sb = new StringBuilder();
        try {
            feedUrl = new URL(params[0]).openConnection();
            InputStream is = feedUrl.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "");
            }
            is.close();

            //createUserPageResult = sb.toString();
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        // if Response from server is OK, then delete the entry from DB TempReports Table
        if (result != null) {
            if (result.equals("OK") || result.equals("updated")) {
                //Toast.makeText(MainActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
                posDatabase.tempLogsDelete(logToDelete);
            }
        } else {
            // don't delete
            //Toast.makeText(MainActivity.this, "NOT Deleted", Toast.LENGTH_SHORT).show();
        }
    }
}

In the logs it says unable to resolve host xx2. Following are the Logs for more clarity.

11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err: java.net.UnknownHostException: Unable to resolve host "xx2": No address associated with hostname
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
11-18 15:30:32.280 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at com.emiturk.pos.MainActivity$sendLogsToServer.doInBackground(MainActivity.java:507)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at com.emiturk.pos.MainActivity$sendLogsToServer.doInBackground(MainActivity.java:493)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.lang.Thread.run(Thread.java:856)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err: Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.io.Posix.getaddrinfo(Native Method)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
11-18 15:30:32.290 13867-14241/com.emiturk.pos W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
11-18 15:30:32.300 13867-14241/com.emiturk.pos W/System.err:    ... 21 more

Answer

pleft picture pleft · Nov 18, 2016

You mentioned that the url is served from your localhost (probably a desktop pc acting as a server in your home network infrastructure), is your localhost named xx2?

You should access it via IP e.g. http://192.168.1.1/.... because your android device is unaware that the name xx2 resolves to your server's ip.