android - React Native fetch API 不适用于 Android
问题描述
我是 React Native 开发的初学者。我开发了我的第一个 React Native 应用程序并在 Android 和 iOS 版本中启动。
在应用程序中,我使用 fetch API 与服务器通信。应用程序运行良好,并从 iOS 版本的 fetch API 获得响应。如果设备连接到高速网络连接(如 WiFi、4G ..),它在 Android 版本中也能正常工作。
但是,如果设备连接的网络连接不足,则 fetch API 不会在 Android 版本的应用程序中返回任何值(出现“网络连接失败”错误)。但在同一连接中,安装在同一设备中的其他应用程序运行良好。
我在用
react-native-cli: version 2.0.1
react-native: version 0.55.4
babel-preset-react-native: version 4.0.0,
Android compileSdkVersion 26
buildToolsVersion 23.0.1
这是我的获取 API 代码
getStudentList() {
var UrlDetails = URLDetails.getURLDetails();
return fetch(UrlDetails.SERVER_NAME + UrlDetails.GET_STUDENT_DETAILS_LIST_URL + 'student_id=1')
.then((response) => response.json())
.then((responseJson) => {
let ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
let isAllSet = responseJson.error;
if (!isAllSet) {
this.setState({
isLoading: false,
dataSource: ds.cloneWithRows(responseJson.student),
isAllSetToShow: isAllSet,
}, function () {
activity_log = "received student details";
this.updateDeviceLog();
});
} else {
this.setState({
isLoading: false,
isAllSetToShow: isAllSet,
}, function () {
activity_log = "received response no student details";
this.updateDeviceLog();
});
}
})
.catch((error) => {
console.error(error);
activity_log = JSON.stringify(error);
Alert.alert('Alert',"Please check your internet connection!");
this.updateDeviceLog();
});
}
任何建议表示赞赏。
解决方案
推荐阅读
- java - 应用程序在点击主页按钮后崩溃,然后通过单击应用程序图标返回相同的活动
- ecmascript-6 - 带有对象的 ES6 模板文字
- tomcat7 - 在 tomcat 中配置 OCSP 的说明
- javascript - 是不是很好用的模式!!用于监听 ReactJS 更改的符号?
- ms-access - Microsoft 访问 - 完成百分比
- ruby-on-rails - rake db:migrate rake 中止!无方法者
- windows - 使用 CMD Start with Parameter 执行 .exe
- node.js - 如何将 unixtime 与 $dateToString mongodb 一起使用
- java - 插入语句后的 Java SQLLite 数据库锁定
- maven - 为什么 maven-site-plugin 总是使用 3.3 版?