getting UnkownHostException when trying to use Retrofit creating an android widget

26 Views Asked by At

I'm trying to create a simple widget that just shows data acquired by a json exposed via Github. I'm using Retrofit (don't know if this is right but i found this one) from the emulator on studio all good but when i generate the apk and install it on my device it wont work and i got this error: java.net.UnkownHostException: Unable to resolve host "host": No Address associated.

I added these to manifest

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

But this is still not working. I even tried with google.com but still wasnt able to resolve the host. What can i try? Is this a Retrofit problem?

Here the retrofit part:

@Keep
object RetrofitService {

    private val numbersRetrofit: Retrofit = Retrofit.Builder()
        .baseUrl("https://host.github.io/")
        .client(OkHttpClient())
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    fun getGamesApiService(): GamesApiService {
        return numbersRetrofit.create(GamesApiService::class.java)
    }

}

Any idea? Thanks

UPDATE: I checked also with this method:

    private fun isOnline(mgr: ConnectivityManager): Boolean {
        val netInfo = mgr.activeNetworkInfo
        return if (netInfo != null && netInfo.isConnected) {
            true
        } else false
    }

And netInfo.isConnected is actually false

1

There are 1 best solutions below

0
Marco On

The solution was adding a file in xml folder named network_security_config.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
 </network-security-config>

Then adding this line in the manifest inside the application tag:

android:networkSecurityConfig="@xml/network_security_config"