Android 9.0 (API 28) Http request Solution

In Beijing on August 7, 2018, Google released the Android 9.0 (API 28), code-named "Pie", which is a new feature: all applications use the default Https, this will lead to the original Http request was invalid. Of course, developers can targetSdkVersion set to 26, but this year's Google play store the new requirements of the application must be submitted targetSdkVersion set to 28, that the application must adapt Https.

 Original link: https: //www.jianshu.com/p/1210191e9895

There are policies on measures to counter: the saying goes. Here we will discuss how to solve the Android 9.0 (API level 28) Http adaptation problems. There are three solutions:

  • APP use Https request (need server support)

  • targetSdkVersion down to 27 or less (including 27)

  • Android network security configuration settings to customize according to their network security

A, APP switch to https request (need server support)

APP will all Http requests are replaced Https, this is the most complete solution, can be considered regular army lines, but this is not a small amount of work, you also need server support, that is, all Http requests server interfaces are also replaced Https, otherwise is invalid, in new projects or important project, it is recommended to do so.

Two, targetSdkVersion down to 27 or less (including 27)

   compileSdkVersion 26
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

  

targetSdkVersion down to 27 or less (including 27), although Google play store for new applications requiring API 28 (2019.08 start), but the major domestic application platforms, such as BAT can basically only require API 26, or go to the end of this policy to take effect, so only the domestic market, it will target version is also set at 26 for a period of time.

Third, customize their network security settings

If the project is involved in the overseas market, which must be on the Google play store Http have to use the interface, I feel very contradictory, of course, is there are ways to deal with, by configuring network security settings can achieve the same effect.

API target set build.gradle in:

   compileSdkVersion 28
    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 28
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

  Set networkSecurityConfig property follows in the manifest file AndroidManifest.xml of the application label inside:

 <application
        ...
        android:allowBackup="true"
        android:icon="${app_icon}"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        ...>
    </application>

  New xml folder under the res directory, create xml file network_security_config.xml as follows:

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

  or

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

  

Guess you like

Origin www.cnblogs.com/1157760522ch/p/11352863.html