發現在模擬器上請求沒問題,但是打包之後,請求失敗,提示錯誤信息network error
axios請求
fetch請求
查閱了很多文檔後,發現在android9.0以上由於安全性高了,所有不做處理http不能使用,解決方法有如下三種:
1. 降級targetSdkVersion的版本,降到27或27以下
2.服務器請求http改爲https (推薦)
3. 兼容http請求
# 在清單文件中設置networkSecurityConfig屬性 <manifest ... > <application android:networkSecurityConfig="@xml/network_security_config"> </application> </manifest> # 在res/xml文件夾下創建network_security_config.xml <network-security-config> <base-configcleartextTrafficPermitted="true"> <trust-anchors> <certificatessrc="system"/> </trust-anchors> </base-config> </network-security-config>
注意!,network_security_config.xml文件第一行不要有空格,再次打包就OK了。