我正在React-Native中创建一个Android应用程序,但是在使用用于注册的API进行抓取时遇到了问题。因此,我尝试在android/app/src/main/assets/xml
react_native_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>
<domain includeSubdomains="false">MyDomain</domain>
</domain-config>
</network-security-config>
然后将其添加到AndroidManifest.xml
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/react_native_config"> // THERE
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
但是当我编译应用程序时,我收到此错误:
android/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml:24: AAPT: error: resource xml/react_native_config not found
你知道我该怎么解决吗?
答案 0 :(得分:0)
要在本地响应中使用fetch API,您无需添加XML文件。只需启用这样的Internet权限
<manifest>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>
并将提取API用作-
获取-
function getMoviesFromApiAsync() {
return fetch(url)
.then((response) => response.json())
.then((responseJson) => {
return responseJson;
})
.catch((error) => {
console.error(error);
});
}
POST-
fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue',
}),
}).then((response) => response.json())
.then((responseJson) => {
return responseJson;
})
.catch((error) => {
console.error(error);
});