问题
Working with requests to the server using Fetch and also using Axios, when running it on Android emulator/devise it shows me the following error:
this is the request code:
fetch(URL,{
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: userEmail,
password: userPassword
}),
})
.then((response) => response.json())
.then((responseJson)=>{
Alert.alert("bien");
console.warn(responseJson);
})
.catch((error)=>{
console.error(error);
console.warn(error);
});
回答1:
Go to info.plist (ProjectFolder->ios->ProjectFolder->info.plist)and add the following before </plist>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Now restart project once again .
回答2:
Well the problem was with the SSL certificate of the URL
回答3:
Since Http request doesn't work in the latest android updates I think after 28 arrived. So you have to add the following attributes to your AndroidManifest.xml
<manifest
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true">
// ----------------
</application>
</manifest>
来源:https://stackoverflow.com/questions/48935563/react-native-http-request-dont-work-in-android