In my Android app Retrofit API requests is working fine in Android 12 and below but is not working on Android 13+ devices

77 Views Asked by At

See my Buid.gradle : App level

I have updated all the dependency to the latest version also set the targetSDK to 34 and compile SDK to 34.

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "dev.panwar.abtax"
    compileSdk = 34

    defaultConfig {
        applicationId = "dev.panwar.abtax"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        viewBinding = true
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.12.0")
    implementation ("com.karumi:dexter:6.2.3")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.11.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.7.0")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
    implementation("androidx.navigation:navigation-fragment-ktx:2.7.7")
//    retrofit
    implementation ("com.squareup.retrofit2:retrofit:2.9.0")
    implementation ("com.squareup.retrofit2:converter-gson:2.9.0")
    implementation ("com.squareup.okhttp3:okhttp:4.9.1")
    

    
    implementation("androidx.navigation:navigation-ui-ktx:2.6.0")
    implementation ("de.hdodenhof:circleimageview:3.1.0")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")


}

I have tried on multiple Android 12 or below devices and API request are working fine but on Android 13+ it's is going to onFailure Function.

val call: Call<LoginResponse> = RetrofitInstance.api.loginUser(email, password)
                call.enqueue(object : Callback<LoginResponse> {
                    override fun onResponse(call: Call<LoginResponse>, response: Response<LoginResponse>) {
                        if (response.isSuccessful) {
                            hideProgressDialogue()
                            val registrationResponse = response.body()
                            if (registrationResponse?.message=="User logged In Successfully"){
//                                saving auth token
                                saveAuthToken(registrationResponse.accessToken)
                                startActivity(Intent(this@LoginActivity,HomeActivity::class.java))
                                finish()
                            }else{
                                Toast.makeText(this@LoginActivity,registrationResponse?.message,Toast.LENGTH_SHORT).show()
                            }
                            Log.e("LoginResponse",response.body().toString())
//                        Toast.makeText(this@SignUpActivity, "${registrationResponse?._id}", Toast.LENGTH_SHORT).show()
                        } else {
                            hideProgressDialogue()
                            val errorBody = response.errorBody()?.string()
                            val jsonObject = JSONObject(errorBody)
                            val errorMessage = jsonObject.getString("message")

                            // Show the error message in your app (e.g., using a Toast or a TextView)
                            // For example, using Toast:
                            Toast.makeText(this@LoginActivity, errorMessage, Toast.LENGTH_LONG).show()
                        }
                    }

                    override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
                        hideProgressDialogue()
                        // Handle failure (e.g., network issues)
                        Toast.makeText(this@LoginActivity, "Request failed: ${t.message}", Toast.LENGTH_SHORT).show()
                    }
                })

What should be the fix?

1

There are 1 best solutions below

1
waseem akram On

Did u apply this in network_security_config.xml i think that in android 13 network securiy in network security configuration or behavior in Android 13+. Android 13 (Tiramisu) introduced new features and behavior changes that might affect network connections.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <!-- Trust preinstalled CAs -->
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

in manifist file

<application
    android:networkSecurityConfig="@xml/network_security_config"
    ...>
    ...
</application>