简体   繁体   English

类型错误:尝试从 android 模拟器访问本地 API 时网络请求失败。 React-Native

[英]TypeError: Network Request Failed when trying to hit local API from android emulator. React-Native

I've seen answers to this question everywhere but none of them seem to work for me.我到处都看到了这个问题的答案,但似乎没有一个对我有用。

I'm trying to do a react native fetch from my android emulator and hit my local API running on https://localhost:5002/v1/users.我正在尝试从我的 android 模拟器中进行反应原生获取,并点击在 https://localhost:5002/v1/users 上运行的本地 API。

This is what my fetch looks like right now:这就是我的 fetch 现在的样子:

var response: Response = await fetch(`https://10.0.2.2:5002/v1/user/${initialContext.userId}`, {
        method: "GET", 
        headers: {
          "Authorization": `Bearer ${initialContext.accessToken}`
        }
      });

and this is the error that I'm getting:这是我得到的错误:

Possible Unhandled Promise Rejection (id: 0):
TypeError: Network request failed
TypeError: Network request failed
    at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:26139:20
    at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:30727:21
    at _callTimer (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:30643:9)
    at Object.callTimers (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:30851:9)
    at MessageQueue.__callFunction (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2781:31)
    at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2513:17
    at MessageQueue.__guard (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2735:13)
    at MessageQueue.callFunctionReturnFlushedQueue (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\index.bundle:2512:14)
    at C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\debuggerWorker.js:199:58
    at process.<anonymous> (C:\Users\raffaele.leo\Downloads\Source-Code\cbt-systems\1.x\trunk\src\CBTravel.Mobile.AirPortal\.vscode\.react\debuggerWorker.js:68:9)

As you can see I've tried running it with 10.0.2.2 (the local host equivalent for the emulator) and I've also tried my computer's IP address.如您所见,我已经尝试使用 10.0.2.2(与模拟器等效的本地主机)运行它,并且还尝试了计算机的 IP 地址。 I've also tried running adb reverse and other port forwarding methods.我也尝试过运行 adb reverse 和其他端口转发方法。

The hardest part for me to wrap my head around is that if I go to https://10.0.2.2:5002 on chrome on my emulator I'm able to hit my local API.对我来说最难理解的部分是,如果我在模拟器上的 chrome 上使用 go 到https://10.0.2.2:5002 ,我就可以访问我的本地 ZDB974Z42387108CA8146 I'm also able to hit an API in the cloud from that fetch statement, just not anything running on local host.我还可以从该 fetch 语句中找到云中的 API,而不是在本地主机上运行任何东西。

Any ideas would be appreciated.任何想法,将不胜感激。

Actually it's easy to work using ngrok , which acts in between your emulator and localhost(network tunnelling).实际上,使用ngrok很容易工作,它在您的模拟器和 localhost(网络隧道)之间起作用。 It provides you with a domain and forwards all your requests to localhost.它为您提供一个域并将您的所有请求转发到 localhost。 You can check this documentation https://ngrok.com/docs您可以查看此文档https://ngrok.com/docs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React-native Android模拟器 - POST因TypeError失败:网络请求失败 - React-native Android emulator - POST fails with TypeError: Network request failed 无法启动模拟器。 原因:模拟器在启动前退出 en React Native when react-native run-android - Failed to launch emulator. Reason: Emulator exited before boot en React Native when react-native run-android 当从Android模拟器调用API响应本机时,显示消息的网络请求失败,但不是在实际设备中? - When call API from android emulator in react native then message shown network request failed but not in real device? React-Native Android“网络请求失败” - React-Native Android 'Network Request failed' 反应原生 android: TypeError: Network request failed - react native android: TypeError: Network request failed Android上的React-Native fetch()网络请求失败 - React-Native fetch() Network Request Failed on Android react-native 的“网络请求失败”(即使使用 `android:usesCleartextTraffic=“true”`) - “Network request failed” with react-native (even with `android:usesCleartextTraffic=“true”`) “TypeError:网络请求失败”(React Native)Android 提取无法与本地后端一起使用 - “TypeError: Network request failed” (React Native) Android fetch fails to work with local backend Android - React Native Integration - TypeError:网络请求失败 - Android - React Native Integration - TypeError: Network request failed React Native TypeError:网络请求失败,fetch() - React native TypeError: Network request failed with fetch()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM