2

I have use The AsyncTask For connecting Internet. a Progress dialog can show at onPreExecute() and i check is Online of that mobile if yes means it will execute the http connections code also dismiss the progress dialog at onPostExecute() and its work as good.

But i have problem if net connection is available at time of Request and connection closed before get Response means the Progress dialog showing always.

Now i want solve this if interconnect disconnect before get Response mean it's alert me as No internet connection and dismiss the progress dialog (may set loading time limit for 30 seconds).

below is my code.

can any one help?

   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();
        }
    }

}
Mohanraj S K
  • 657
  • 4
  • 16

3 Answers3

1

You can use broadcast receiver to get the event of network connect or disconnect. register this receiver in doInbackground method and unregister it in onPostExecute method.

 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); 
Bhawna Raheja
  • 524
  • 6
  • 9
1

If the connection will be closed before or while the http request an IOException will be thrown. Catch that exception and close your Dialog there and inform the user about this event.

  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();
        } 
  }

I don't know if you really need a return value to your onPostExecute(). If yes consder to make the logic aware that an exception could occur.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
0

i thank to Steve Benettand Bhawna Raheja, sorry for the late reply, Simply i have solve this error by set Timeout via HttpRequest.TimeOut . i dont know how i forgot to set time out.

One again thank to You both.

Mohanraj S K
  • 657
  • 4
  • 16