调用连续Web服务android的更好方法是什么?

时间:2016-10-27 10:44:05

标签: java android web-services

在应用程序中,我连续调用多个Web服务。例如,用户首先登录并成功调用,我在成功调用上调用'Check Available'服务我称之为'下载Web服务'以成功获得响应然后我如果互联网中断或服务器停止响应我会显示一条消息(互联网中断/服务器没有响应),在登录成功通话时会发生什么。我想在互联网连接后继续或恢复进程(意味着它应该继续重试)。为此,我捕获异常SocketTimeOut和其他异常,最后我检查是否发生异常,给回调用类和回调方法我停止线程5秒后再次调用webservice(经常无效调用)。我的问题是什么应该是webservice的这个序列调用更好的方法。我已经实施了它更好吗?

1 个答案:

答案 0 :(得分:0)

我认为您应该倾听连接的变化而不是每5秒钟尝试一次。手机可以离线数小时。

某处,例如应用onCreate

registerReceiver(
      new ConnectivityChangeReceiver(),
      new IntentFilter(
            ConnectivityManager.CONNECTIVITY_ACTION));

在另一堂课:

public class ConnectivityChangeReceiver
               extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      debugIntent(intent, "grokkingandroid");
   }

   private void debugIntent(Intent intent, String tag) {
      Log.v(tag, "action: " + intent.getAction());
      Log.v(tag, "component: " + intent.getComponent());
      Bundle extras = intent.getExtras();
      if (extras != null) {
         for (String key: extras.keySet()) {
            Log.v(tag, "key [" + key + "]: " +
               extras.get(key));
            // Do whatever you need based on connectivity status
            // You could send an internal broadcast that your web service code listens for
         }
      }
      else {
         Log.v(tag, "no extras");
      }
   }

}

取自: http://www.grokkingandroid.com/android-getting-notified-of-connectivity-changes/

这个主题有很多资源。

相关问题