Android 获取Api:无法从本地主机获取数据
我一直在尝试使用nativescript创建一个android应用程序。我正在使用fetch模块从我的服务器获取响应。当我尝试从httpbin.org/get获取响应时,这是正常的。但当我尝试从本地服务器获取响应时,我获取的网络请求失败。错误 发送到httpbin.org/get-Android 获取Api:无法从本地主机获取数据,android,node.js,laravel-5.2,nativescript,fetch-api,Android,Node.js,Laravel 5.2,Nativescript,Fetch Api,我一直在尝试使用nativescript创建一个android应用程序。我正在使用fetch模块从我的服务器获取响应。当我尝试从httpbin.org/get获取响应时,这是正常的。但当我尝试从本地服务器获取响应时,我获取的网络请求失败。错误 发送到httpbin.org/get- return fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(fu
return fetchModule.fetch("https://httpbin.org/get").then(response => { return response.text(); }).then(function (r) {
console.log(r);
}, function (e) {
console.log(e);
});
发送到本地主机:8000/api-
return fetchModule.fetch("http://localhost:8000/api").then(response => { return response.text(); }).then(function (r) {
console.log(r);
}, function (e) {
console.log(e);
});
当我试图通过请求模块从纯node.js中的localhost:8000/api获得响应时,它工作得很好。但是现在,我不知道如何在nativescript中使用fetch模块解决这个问题。相反,
localhost
(通常127.0.0.1
)使用10.0.2.2(如果使用AVD emulator)
对于GenyMotion emulator,环回地址为10.0.3.2emulator API路径:-。就像您的API本地路径是或一样http://localhost:5000/api 然后替换为,端口地址与本地API端口相同
路径-App_resources/Android/src/main/AndroidManifest.xml:-
android:usesCleartextTraffic=“true”加载项应用程序标记你确定你的手机上有internet连接吗?不要使用localhost作为本地主机指向本地设备,在这种情况下,你的手机不是服务器。在您的电脑上检查什么是IP,并将localhost更改为IPWhy Internet连接?我使用的是Android Studio的AVD。看到这个答案了吗?localhost指的是设备(仿真器),而不是运行仿真器的计算机的IP。像这样-10.0.2.2/api或10.0.2.2:8000/api这样?请在这里帮助我@user145959你应该使用
https://10.0.2.2
instaedhttp://localhost
。。。IP地址192.168.x.x(表示本地网络中的一个地址)与环回地址无关,所以我不确定您为什么要首先使用它。@NickIliev:我更改了我的代码,就像导出constbaseURL=”https://10.0.2.2:3000";代码>但不起作用。我更新了我的问题
10.0.2.2 - Special alias to your host loopback interface
(i.e., 127.0.0.1 on your development machine)