[英]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.