简体   繁体   English

React-native - Axios GET请求在Android模拟器中引发网络错误

[英]React-native - Axios GET request throws network error in Android emulator

I am new to React-native. 我是React-native的新手。 I am trying to perform a simple get request using the following code: 我正在尝试使用以下代码执行简单的get请求:

axios.get('http://push-cycling-backend.dev/api/test')
    .then((response) => {
        console.log(response);
    })
    .catch((error) => {
        console.log(error);
    });

I have tested the request using Postman and everything works fine, but when I run the request in my react-native app using the ANDROID EMULATOR, I get the following error: 我已经使用Postman测试了请求并且一切正常,但是当我使用ANDROID EMULATOR在我的react-native应用程序中运行请求时,我收到以下错误:

Error: Network Error 错误:网络错误

I have a feeling this is because I am trying to access a Api end point hosted locally through Xammp via the Android emulator, but I have no idea what to do. 我有一种感觉这是因为我试图通过Android模拟器访问通过Xammp本地托管的Api端点,但我不知道该怎么做。 Please any guidance would be greatly appreciated. 请非常感谢任何指导。 Kind regards, Matt 亲切的问候,马特

I managed to get it working by changing the request URL from http://push-cycling-backend.dev/api/test to http://10.0.2.2:80/push-cycling-backend/public/api/test . 我设法通过将请求URL从http://push-cycling-backend.dev/api/test更改为http://10.0.2.2:80/push-cycling-backend/public/api/test来使其工作。 From what i understand, since the android emulator, emulates a actual device, I believe 'localhost' was referring to an address on the emulated device instead of my local server 10.0.2.2 is something like a keyword that refers to your local machines ip address. 从我的理解,自从Android模拟器,模拟一个实际的设备,我相信'localhost'指的是模拟设备上的地址而不是我的本地服务器10.0.2.2就像一个关键字,指的是你的本地机器的IP地址。

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

相关问题 react-native提取在具有HTTP远程服务器后端的Android设备上引发“网络请求失败”错误 - react-native fetch throws “Network request failed” error on Android Devices with backend in http remote server 在 Android 物理设备上反应本机 axios 后请求网络错误 - React native axios post request network error on Android physical device React-Native Android“网络请求失败” - React-Native Android 'Network Request failed' 类型错误:尝试从 android 模拟器访问本地 API 时网络请求失败。 React-Native - TypeError: Network Request Failed when trying to hit local API from android emulator. React-Native React-native Android模拟器 - POST因TypeError失败:网络请求失败 - React-native Android emulator - POST fails with TypeError: Network request failed Fetch 在 React-Native 0.14 中抛出“网络请求失败” - Fetch throws "Network request failed" in React-Native 0.14 Axios React Native 网络错误 Android Expo - Axios Network Error in React Native with Expo in Android 反应本机提取和 axios 不在 android 模拟器上发送请求 - react native fetch and axios is not sending request on android emulator 位置请求在具有React-Native的Android模拟器上超时 - Location request timed out on android emulator with React-Native Android上的React-Native fetch()网络请求失败 - React-Native fetch() Network Request Failed on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM