Android P network requests associated summary

This first half of the several mobile phone manufacturers can be called gods fight, millet 9, iqoo, Huawei, oppo, Samsung and so on are starting Android P system. It also considered several times and finally put on a new phone, but also to experience this Android9.0 (turned out to be one of nubia Android6). Then wait a bit run their written several app, and then run this on the problem.

Android 9 wifi transmission

In Android9 Pentax camera is connected to the top of wifi time, but can not transfer files, you can not get SLR sd card thumbnails, it will be reported the following error:

No Network Security Config specified, using platform default

Later, by referring to the official website, we found that Google expressly prohibited http transmission Android P above, that is, the default is to use https instead of http. But also gives the official solution. Can refer to the official information, I am here to briefly summarize

1. Add security profile
First create a file network_security_config.xml file in res / xml folder, and then add the domain can be trusted or ip.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">example.com</domain>
    </domain-config>
</network-security-config>

You can use plain text or add default transmission. After adding the following properties in the application of AndroidMainfest.xml

android:networkSecurityConfig="@xml/network_security_config"
  1. Reduce api version, in 27 or less can be used in clear text http transport. But as a developer is not so much sense.

Build their own backend access

Another project is to build their own web server access via Android. Also there will be an error by the above two methods can solve the problem. There is also a third way, that is to manually add ssl certificate

  1. spring boot configuration ssl https implement access credentials
    may be used java own certificate generation tool to achieve, first open cmd terminal, an input command to generate a certificate ssl
keytool -list -keystore server.p12

After less than six will be prompted to enter the password, and then also need to re-enter, and fill in other information. Follow the steps on the line, the key is to remember the password. spring boot, add a few configuration in the configuration file in application.properties

#需吧生成的文件放在与配置文件相同目录下
#server.ssl.key-store=classpath:keystore.p12
#配置证书密码
#server.ssl.key-store-password=111111 
##server.ssl.keyStoreType=PKCS12
#可以手动指定端口,否则使用默认的8443端口
#server.ssl.keyAlias:tomcat

After the restart project can see the server!

Because the certificate is invalid certificate is so sure of his own creation

Add the trust certificate

After I ran the test using java when they reported such a mistake

avax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present

This is because after you have added if you use https access java.net or Android, then you need to add or trust. java application, then you need to add a certificate to support. Binary files can be downloaded from the website. Click Copy to file a certificate after the first click on the map

After clicking Next, select the certificate der file format. Use this command to import the certificate to the local library cacerts

keytool -import -alias vbooking -keystore cacerts -file ${JAVA_HOME}/jre/lib/security/vbooking.cer

After running java test whether it is local or, to use local java.net.HttpURLConnection it is not being given. Android end if res / raw / my_ca added DER format certificate that you just created in the network security configuration file.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">example.com</domain>
        <trust-anchors>
            <certificates src="@raw/my_ca"/>
        </trust-anchors>
    </domain-config>
</network-security-config>

Which point to note is that the use of the Internet in the Android client request can not be used when a network request ui in the main thread, should be called in AsyncTask, regarding prior to use AsyncTask also summed up. Another point is the need to request permission to add a network.

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

Android development information architecture + interview information click on the link to receive free share

"Android Architect necessary to receive a free learning resources (architecture + video + interview thematic document study notes)"

Guess you like

Origin blog.csdn.net/Coo123_/article/details/93209759