React native iOS设备| |反应本机| | expo上的网络请求失败 我想做的是:
我正在尝试使用我的ios设备在expo上的React Native应用程序向本地API(使用ApiPlatform创建)发送请求。 有人知道怎么修吗?:) 下面是我想做的请求: “” 工作原理: 我的请求在我的计算机上运行 我的请求通过键入以下内容在android emulator上工作: “” 我所做的: 对于expo上的ios设备, 我读到我需要通过计算机的Ipv4更改127.0.0.1:8000, 所以我跑了React native iOS设备| |反应本机| | expo上的网络请求失败 我想做的是:,react-native,expo,api-platform.com,React Native,Expo,Api Platform.com,我正在尝试使用我的ios设备在expo上的React Native应用程序向本地API(使用ApiPlatform创建)发送请求。 有人知道怎么修吗?:) 下面是我想做的请求: “” 工作原理: 我的请求在我的计算机上运行 我的请求通过键入以下内容在android emulator上工作: “” 我所做的: 对于expo上的ios设备, 我读到我需要通过计算机的Ipv4更改127.0.0.1:8000, 所以我跑了 ipconfig 在powershell中,获取我的Ipv4地址,输入我的请求
ipconfig
在powershell中,获取我的Ipv4地址,输入我的请求:
“”
我读到ios不允许http协议,所以我在app.json->infoPlist->NSAppTransportSecurity->true中添加了
"ios": {
"supportsTablet": true,
"infoPlist": {
"NSAppTransportSecurity" : {
"NSAllowsArbitraryLoads" : true,
}
}
}
。就像所有其他主题所说的,但总是相同的错误:“网络请求失败”
我连接在同一个网络中(iOS设备和android emulator)
例如,我尝试了“”,它可以在我的模拟器和iOS设备上运行
我使用的是其他API,但以“https”开头。这里的问题似乎是“http”
但是,我尝试通过在app.json的infoPlist中添加“NSAppTransportSecurity”:“true”来修复它,但没有任何更改。就像我说的
我还尝试在另一个家连接到internet,更改IPv4。但问题仍然存在
我看不出其他办法来解决它。我可能错过了什么
这里是允许请求的函数
export function getsBeersFromRatio(){
const url = Platform.OS === 'android'? 'http://10.0.2.2:8000/api/beers?abv%5Bgte%5D=6.2' : 'http://172.20.10.2:8000/api/beers'
return fetch(url)
.then((response) => response.json())
.catch((error) => console.log(error));
}
_searchBeersFromRatio(){
this.setState({
beers: [],
},
() => {
this._loadBeersFromRatio()
})
}
_loadBeersFromRatio(){
console.log('====================================');
console.log(' NEW REQUEST ');
console.log('====================================');
this.setState({ isLoading: true })
getsBeersFromRatio().then(data => {
console.log(data);
})
}
<Button
title = "get data from API"
onPress = {() => this._searchBeersFromRatio()}
/>
这里是我的app.json
{
"expo": {
"name": "BierRatio",
"slug": "BierRatio",
"privacy": "public",
"sdkVersion": "33.0.0",
"platforms": [
"ios",
"android",
"web"
],
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true,
"infoPlist": {
"NSAppTransportSecurity" : {
"NSAllowsArbitraryLoads" : true,
}
}
}
}
}
预期行为
我期望的日志与我的android仿真器相同
实际行为
实际输出为“网络请求失败”
谢谢! 解决方法:
我终于找到了解决办法:
当我在电脑上键入http://“我的IPv4地址”:8000/“我的Api”时,他说:“错误连接被拒绝”
所以我尝试运行$php bin/console服务器:运行“我的IPv4地址”:8000
防火墙通知出现在我面前,我接受它。
我在我的iOS设备中再次发送请求,但我不知道为什么它现在能工作
所以我需要跑步
$php bin/console服务器:运行
在另一个超级地狱里
$php bin/控制台服务器:运行“我的IPv4地址”:8000
在我的android emulator和iOS设备中访问我的本地API。解决方法:
我终于找到了解决办法:
当我在电脑上键入http://“我的IPv4地址”:8000/“我的Api”时,他说:“错误连接被拒绝”
所以我尝试运行$php bin/console服务器:运行“我的IPv4地址”:8000
防火墙通知出现在我面前,我接受它。
我在我的iOS设备中再次发送请求,但我不知道为什么它现在能工作
所以我需要跑步
$php bin/console服务器:运行
在另一个超级地狱里
$php bin/控制台服务器:运行“我的IPv4地址”:8000
要在android emulator和iOS设备中访问我的本地API。我遇到了完全相同的问题,我使用
fetch()
将我的数据从本机react应用程序发送到我的php服务器,我使用expo从iOS手机进行模拟,但在配置正确的IPV4地址时,我收到了与您相同的错误
fetch (http: // ipv4 address / ...)
而不是本地主机
当我在电脑上模拟网络视图时
fetch (http: // localhost / ..)
我收到的数据一切正常。我遇到了完全相同的问题,我使用
fetch()
将我的数据从我的本地react应用程序发送到我的php服务器,我使用expo从我的iOS手机模拟,但当我配置了正确的IPV4地址时,我收到了与您相同的错误
fetch (http: // ipv4 address / ...)
而不是本地主机
当我在电脑上模拟网络视图时
fetch (http: // localhost / ..)
我收到数据,一切正常