用UnityWebRequest发送请求时,用编辑器发送正常,打出Android包再请求就报出UnknownError,查了下是因为Android 28以后不允许直接发送明文,网上查了下有几种解决方案,这里记录一下:
Android本机API确实允许HTTP,至少可以使用curl库。但是,Java API(这是UnityWebRequest当前在幕后使用的)不允许HTTP
解决办法:
在AndroidManifest.xml中添加权限
1、需要在清单文件中更新此行android:usesCleartextTraffic =“ true”
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" xmlns:tools="http://schemas.android.com/tools" android:installLocation="preferExternal"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" /> <application android:theme="@style/UnityThemeSelector" android:icon="@mipmap/app_icon" android:label="@string/app_name" android:usesCleartextTraffic="true"> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> </manifest>
来源:https://blog.csdn.net/www1501766557/article/details/106837049/