Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve host: URL No address associated with hostname

I have an web service call to bring down a JSON object and it only works if I have the IP address and not the host name. I have been fine using IP addresses but now I need to have the host name.

Here is my code

    StringBuilder stringBuilder = new StringBuilder();
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
    HttpConnectionParams.setSoTimeout(httpParams, 7000);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
    HttpPost httpost = new HttpPost(URL);
    try {
        HashMap<String,String>  params = new HashMap<String,String>();
        // Login info
        params.put("username", edtUserName.getText().toString()); 
        params.put("password", edtPass.getText().toString());
        params.put("deviceOS", "Android");
        params.put("deviceOSVersion", android.os.Build.VERSION.RELEASE);
        params.put("language", "0");
        //JSON object holding the login info
        JSONObject holder = new JSONObject(params);

        StringEntity se = new StringEntity(holder.toString());

        httpost.setEntity(se);

        httpost.setHeader("Content-Type", "application/json");
        //Executing the call
        HttpResponse response = httpClient.execute(httpost);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
        } else {
            returnCode = -1;
            Log.d("JSON", "Failed to download file");
        }
    } catch (Exception e) {
        returnCode = -1;
        Log.d("readJSONFeed", e.getLocalizedMessage());
    }

    return stringBuilder.toString();

I dont know why that doesn't work. I am testing on a phone with Wifi connection, My manifest has the <uses-permission android:name="android.permission.INTERNET" /> and <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This web service does work. Just not on Android

If anyone has an idea that would be great, Thanks.

like image 746
BigT Avatar asked Sep 12 '13 14:09

BigT


People also ask

What does it mean unable to resolve host?

This error indicates that the domain name servers (DNS) that translate your domain name into an IP address failed to respond during the test cycle.

Can't resolve host Kali Linux?

Solution: Please make sure you have an active Internet connection and your Kali DNS settings are correct. We recommend using Google public DNS servers. Please set both DNS servers to 8.8.

Could not establish connection to could not resolve hostname?

The ssh could not resolve hostname is a common error that any user can face. It is not that big of an issue. It simply happens when the ssh command fails to resolve the IP address of the provided hostname. In such situations clearing the DNS cache or editing the /etc/host file can help.


1 Answers

I fixed it by removing these lines

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpConnectionParams.setSoTimeout(httpParams, 7000);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);

And replacing it with this code

HttpClient httpClient = MainActivity.createHttpClient();


public static HttpClient createHttpClient(){
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 15000);
        HttpConnectionParams.setSoTimeout(params, 5000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}
like image 83
BigT Avatar answered Sep 29 '22 17:09

BigT