Retrofit2 response null on android pie

2019-08-04 00:58发布

问题:

On android pie i am getting null response for retrofit2 request. Same code is working on all previous android devices. i have added android:usesCleartextTraffic="true" in manifest its fixed the issue of http connection but i am not getting response as expected in all other androided version the response is expected but not in pie. so please help thanks in advance

  Manifest.xml  
    <application
    android:allowBackup="false"
    tools:replace="android:allowBackup"
    android:icon="@mipmap/cabad_logo_new"
    android:label="@string/app_name"
    android:networkSecurityConfig="@xml/network_security_config"
    android:roundIcon="@mipmap/cabad_logo_new_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">

MainActivity

RetrofitInterface jsonPostService = 
ServiceGenerator.createService(RetrofitInterface.class, 
"http://xxx.xxx.xxx.xxx:8080/");
    Call<LoginResult> call = jsonPostService.postRawJSON(jsonObject);
    call.enqueue(new Callback<LoginResult>() {

        @Override
        public void onResponse(Call<LoginResult> call, 
Response<LoginResult> response) {
            try {

above code is working fine in all android devices except pie. Pie getting null response

回答1:

Go to Manifest.xml in application tag. Add these two lines as given below.

android:usesCleartextTraffic="true"
       tools:targetApi="m


<application
           android:allowBackup="true"
           android:hardwareAccelerated="true"
           android:icon="@mipmap/ic_launcher"
           android:label="@string/app_name"
           android:roundIcon="@mipmap/ic_launcher_round"
           android:supportsRtl="true"
           android:theme="@style/AppTheme"
           android:usesCleartextTraffic="true"
           tools:targetApi="m">

I hope It will work for you.



回答2:

android:usesCleartextTraffic="true",tools:targetApi="m"

add these two line in your manifest inside application tag

`<application
android:allowBackup="false"
tools:replace="android:allowBackup"
android:icon="@mipmap/cabad_logo_new"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/cabad_logo_new_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:targetApi="m"
android:usesCleartextTraffic="true">'