Solution to the failure of Android studio to access HTTP using okgo’s post request

A few days ago, I tried to upgrade an app on the old mobile phone (version is Android 7). It has been normal to log in to the server with okgo's post request. Today, the old mobile phone is not around. When debugging with your own mobile phone, there is a problem of network request failure. Play the toast prompt written by yourself in onerror(). Repeatedly checked that the mobile phone does not have the network permission to turn off the app, and USB debugging is turned on. So I searched some methods on the Internet and tried to solve the problem. Record these two solutions here.

In build.gradle, reduce the targetsdkversion from 30 to below 27 (that is, below Android 8.0), sync now, or click sync project with gradle files in file to synchronize the reference library, and then restart the app. It is found that the network request is OK, and the problem is solved.

You can also choose not to reduce the targetsdkversion version, directly add an XML file under res and create a file named network_ security_ Config.xml file (name is optional), the content is:

Then add attributes in the application tag under the androidmanifest.xml file

The example is shown in the figure below:

Run the program again, the network request is successful, and the problem is solved.

After Android 8 strengthens the system security permission, Android P limits the network requests of plaintext traffic, and the non encrypted traffic requests will be directly prohibited by the system. At present, the network access is basically replaced by HTTPS from the original HTTP in order to strengthen security. If the request of the current application is an HTTP request instead of HTTPS, the system will prohibit the current application from making the request. If the URL of WebView uses HTTP protocol, it will also fail to load, and the content of WebView will not be displayed, and HTTPS will not be affected.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>