React Native Http request dont work in Android

非 Y 不嫁゛ 提交于 2020-01-06 02:23:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!