Determine if internet connection is available

自古美人都是妖i 提交于 2019-12-19 10:33:25

问题


how do you folks check if the device is connected to the internet in a sencha touch app?


回答1:


There's an attribute called navigator.onLine (general browser support, not specific for Sencha)

If I'm in a PhoneGap application (which you often are if you're using Sencha Touch), I'd rather use their network.isReachable function, since I've by experience found it more reliable.

There's also something called 'Offline events', John Resig describes them on his blog: http://ejohn.org/blog/offline-events/.




回答2:


If there is no network connection during an ajax request sencha touch will return a response with a 0 status. So this is what we're using in our app (works in phonegap too):

// global error handling
Ext.Ajax.on('requestexception', function(connection, response) {
  // get rid of any loading masks that are present
  Ext.each(Ext.query('.x-mask'), function(el) {
      new Ext.Element(el).hide()
  });
  switch(response.status) {
    case 0 :
      Ext.Msg.alert('Error Loading', 'No Internet connection.');
      break;
    case 401 :
      Ext.Msg.alert('Login Failed', 'Check your username and password.');
      break;
    default :
      Ext.Msg.alert('Whoops!', 'An unexpected error has occurred and we have been alerted.');
  }
});

Note that this is for touch 1.1, haven't tried in in 2.0 yet.



来源:https://stackoverflow.com/questions/5728346/determine-if-internet-connection-is-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!