【问题标题】:How to set time limit for waiting internet Connection in android AsyncTask?如何在android AsyncTask中设置等待互联网连接的时间限制?
【发布时间】:2014-07-23 05:46:13
【问题描述】:

我使用 AsyncTask 来连接互联网。 进度对话框可以在 onPreExecute() 处显示,如果是,我检查该手机是否在线,如果是,则意味着它将执行 http 连接代码并在 onPostExecute() 处关闭进度对话框,并且它的工作正常。

但如果网络连接在 Request 时可用并且连接在获取 Response 之前关闭意味着始终显示进度对话框,我会遇到问题。

现在我想解决这个问题,如果在获得响应之前互连断开意味着它提醒我没有互联网连接并关闭进度对话框(可能将加载时间限制设置为 30 秒)。

下面是我的代码。

有人可以帮忙吗?

   public class SubjectTask extends AsyncTask<Void, Void, Integer> {

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(Login.this, "Loading",
                "Please wait...");
        //checkConnection();

    }

    @Override
    protected Integer doInBackground(Void... arg0) {

        if (isOnline()) {  //using ConnectivityManager And Network Info

            try {

                //Http Request connections

            } catch (Exception e) {
                e.printStackTrace();
            }



            return 1;
        } else {

            alert("Check your internet connection");
            return 0;
        }

    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

}

【问题讨论】:

    标签: java android-asynctask progressdialog android-internet


    【解决方案1】:

    您可以使用广播接收器来获取网络连接或断开连接的事件。在 doInbackground 方法中注册这个接收者,在 onPostExecute 方法中注销它。

     broadcastReceiver = new BroadcastReceiver() {
    
                        @Override
                        public void onReceive(Context context, Intent intent) {
    
                            ConnectivityManager connectivity = (ConnectivityManager) context
                                    .getSystemService(Context.CONNECTIVITY_SERVICE);
    
    
                                NetworkInfo info = connectivity.getActiveNetworkInfo();
                                //Play with the info about current network state
                                 if(info.getState()== NetworkInfo.State.DISCONNECTED) {
                                   // hide loader and show alert here
    }
    
                            }
    
                        }
                    };
    
                    intentFilter = new IntentFilter();
                    intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
                    registerReceiver(broadcastReceiver, intentFilter); 
    

    【讨论】:

      【解决方案2】:

      如果在 http 请求之前或期间关闭连接,则会抛出 IOException。捕获该异常并在那里关闭您的对话框并通知用户此事件。

        if (isOnline()) {  //using ConnectivityManager And Network Info
      
              try {
      
                  //Http Request connections
      
              } catch (IOException e) {
                  e.printStackTrace();
                  // Do the error handling here
      
              } catch (ClientProtocolException e) {
                  e.printStackTrace();
              } 
        }
      

      我不知道您是否真的需要 onPostExecute() 的返回值。如果是,请考虑让逻辑知道可能发生异常。

      【讨论】:

        【解决方案3】:

        感谢 Steve Benettand Bhawna Raheja, 这么晚才回复很抱歉, 只需通过 HttpRequest.TimeOut 设置 Timeout 即可解决此错误。 我不知道我是怎么忘记设置时间的。

        再次感谢你们俩。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-04-01
          • 1970-01-01
          • 2020-10-29
          • 2017-11-25
          • 2011-09-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多