我有一个已开发的离子移动应用程序,但在互联网连接不良的手机上使用时会引发很多连接错误。
有没有办法可以优化我的应用程序,以便在互联网连接较差的设备上正常运行。即(GPRS或EDGE连接)
答案 0 :(得分:0)
您可以将您的http调用转换为包含更长的超时(此处为3分钟,以毫秒为单位)。还要确保正常处理错误情况。
this.http.get(url)
.timeout(180000)
.map(...)
.subcribe(
(success) => {...},
(err) => { // Deal here with timeout error.
}
);
还要确保使用ionic-storage
缓存任何可以重复使用的数据,如字体,图像,JSON响应等。
答案 1 :(得分:0)
使用网络插件:
https://ionicframework.com/docs/native/network/
您可以查看网络更改事件:
// watch network for a disconnect
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
console.log('network was disconnected :-(');
});
连接类型:
function checkConnection() {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
也许您应该根据用户是否具有连接和用户连接类型来管理您的请求。