Search code examples
androidexceptionhttpurlconnection

Why does Android 10 HTTPUrlConnection fail? Worked fine on 9


With this code

  URL url = new URL(data.replaceAll(" ", "%20"));
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setReadTimeout(12000);
        urlConnection.setConnectTimeout(12000);
        int xy = urlConnection.getResponseCode();

I'm getting this exception:

data = "http://myrpd.us/api/staff1?lang=eng"
urlConnection = {HttpURLConnectionImpl@11410}    "com.android.okhttp.internal.huc.HttpURLConnectionImpl:http://myrpd.us/api/staff1?lang=eng"
e = {UnknownHostException@11411} "java.net.UnknownHostException: Unable to resolve host "myrpd.us": No address associated with hostname"

The URL works fine on 9 and in a browser. I've read all the privacy information that's new with 10. I have this in the manifest:

android:usesCleartextTraffic="true"

Solution

  • This error may also occur when there is no Internet connection

    java.net.UnknownHostException: Unable to resolve host 
    

    Can you check the Internet connection on your Android 10 device?