当我使用react-native init (RN版本0.29.1)创建一个全新的项目,并在渲染方法中获取公共facebook演示电影API时,它抛出了一个网络请求失败。有一个非常无用的堆栈跟踪,我不能调试网络请求在chrome控制台。这是我发送的fetch:

fetch('http://facebook.github.io/react-native/movies.json')
      .then((response) => response.json())
      .then((responseJson) => {
        return responseJson.movies;
      })
      .catch((error) => {
        console.error(error);
      });

当前回答

对于我们来说,这是因为我们正在上传一个文件,而RN filePicker没有给出正确的mime类型。它只是给了我们image作为类型。我们需要将其更改为'image/jpg'以使取回工作。

form.append(uploadFileName, {
  uri : localImage.full,
  type: 'image/jpeg',
  name: uploadFileName
 })

其他回答

“依赖”:{“反应”:“17.0.2”、“react-native”:“0.66.1”}, 我在使用Android模拟器时遇到了这个问题。

将这些代码行添加到AndroidManifest.xml中 <应用程序 .... .... android: usesCleartextTraffic = " true " > 然后,试着在一个真正的物理设备上运行你的代码,而不是模拟器, 要在物理设备上运行,请连接usb并尝试运行NPX react-native run-android

这对我有用,android使用一个特殊类型的IP地址10.0.2.2然后端口号

import { Platform } from 'react-native';

export const baseUrl = Platform.OS === 'android' ?
    'http://10.0.2.2:3000/'
: 
'http://localhost:3000/';

在AndroidManifest.xml中添加android:usesCleartextTraffic="true"行。 删除android文件夹中的所有调试文件夹。

我使用localhost作为地址,这显然是错误的。在将其替换为服务器的IP地址(在仿真器所在的网络中)后,它可以完美地工作。

Edit

在Android Emulator中,开发机地址为10.0.2.2。更多解释在这里

对于Genymotion,地址是10.0.3.2。更多信息请点击这里

检查您的SSL是否正确配置,将url放在这里。