Problem in get data on using retrofit2 on newer Android

218 views Asked by At

I am using retrofit2 in kotlin to connect to the api I get data from API without any problems in Android API 22 But when I test the same program on Android API 30, it gives me the following answer and does not return any value in the API answer.

D/OkHttp: --> POST http://Examples.com/api/login?email=xxxxx%40yahoo.com&password=123
    Content-Length: 0
D/OkHttp: Accept: application/json
D/OkHttp: --> END POST (0-byte body)

I do the following

fun client() : OkHttpClient{
    return OkHttpClient.Builder().
        addInterceptor(HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }).
    connectTimeout(mConnection_TimeOut, TimeUnit.SECONDS).
            writeTimeout(mConnection_TimeOut, TimeUnit.SECONDS).
            readTimeout(mConnection_TimeOut, TimeUnit.SECONDS).build()
}

fun getRetrofit() : Retrofit
{
    return Retrofit.Builder().
            addConverterFactory(GsonConverterFactory.create()).baseUrl(Const.BASE_URL).
            client(client())
        .build()
}

This problem occurs on both Emulator and mobile phones

1

There are 1 answers

0
Mohammad Nikouei On BEST ANSWER

If you have trouble retrieving information from retrofit2 on newer Android, just add the following value to the file AndroidManifest.xml

<application
    . . .  .
    android:usesCleartextTraffic="true"
    tools:targetApi="m">