React Native Fetch / Axios API调用总是会被捕获

苏伯汉·查特吉

我尝试过使用提取和axios。但是这两个调用在我的本机应用程序中均失败。它总是进入catch函数。虽然从浏览器或Postman调用此api效果很好。

axios.get('http://sahajdevapi.letsendorse.com')
.then(function (response) {
  console.warn("res 1: ", response);
}).catch(function (error) {
  console.warn("err 1: ", error);
});

我从catch函数得到的错误如下:

'err:', { [TypeError: Network request failed]
line: 24115,
column: 31,
sourceURL: 'http://localhost:8081/index.delta?platform=android&dev=true&minify=false'}
哈迪克·维拉尼

对于较新的版本,您需要添加一些安全性以进行http访问。

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    <application
        android:name=".MainApplication"
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@drawable/ic_launcher_round"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        tools:ignore="GoogleAppIndexingWarning">

        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" />

        <activity
            android:name=".MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            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>

</manifest>

检查你的文件,你需要在清单标签添加toolsuses-library为。

试试这个,它将起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

react native中的axios和fetch有什么区别?

Django + React -> fetch / axios

React-Native Fetch API主动缓存

来自 fetch API 响应的 React Native flatlist

React Native Fetch异步

Fetch / Axios在React Native中严重崩溃(但仅适用于某些URL)

React Native Fetch API调用未发出请求

HTTP fetch 请求总是在 react-native 上失败

React Native fetch,意外的令牌

React Native FlatList没有取消Axios API请求

React Native FlatList没有显示来自API的数据(axios)

无法使用 API 中的 Axios 在 React Native 中查看数据

在类方法 React Native 中包装 fetch API

React Native Fetch无法在本地使用laravel API

如何使用React Native Fetch()从API检索格式错误的json

无法读取我的 API 或特定 API [React.js/Node.js/Axios/Fetch]

如何在与Jest的react-native中使用模拟的fetch()对API调用进行单元测试

Axios Post Response React Native和Mockable

React-Native:iOS上的Axios问题

React native、redux 和 axios:UnhandledPromiseRejectionWarning

react-native fetch()。then()返回对象Object

无法在React Native中使用`fetch()`

.catch() 在 react-native 的 fetch 中

React Native fetch vs XMLHttpRequest 性能

对 Microsoft Azure 的 React Native fetch 请求失败

React Native 中的 JSON Fetch 解析问题

axios 和 fetch 在 React 中返回空响应数据

Axios(在React-native中)不在本地主机中调用服务器

从另一个页面React native返回带有fetch调用函数的结果值