ekjyot
ekjyot

Reputation: 2227

Android: operation time out when web service called alert dialog not being shown

I want to show a alert dialog if there is low internet connectivity.I am using following code for it:

    try{

            DefaultHttpClient hc = new DefaultHttpClient();
            ResponseHandler<String> res = new BasicResponseHandler();
            HttpPost httppost = new HttpPost(Constants.getHostString() + "/apps_templates.jsp");
            List<NameValuePair> NVP = new ArrayList<NameValuePair>();
            NVP.add(new BasicNameValuePair("requester", "android"));
            NVP.add(new BasicNameValuePair("device", device));
            httppost.setEntity(new UrlEncodedFormEntity(NVP));
            String response = hc.execute(httppost, res);
            //long t2 = System.currentTimeMillis();
            //long elapse = t2 - t1;
            //System.out.println("elapse time is"+elapse);
              HttpConnectionParams.setConnectionTimeout(hc.getParams(), 30000);
                int timeoutSocket = 30*1000;
                HttpConnectionParams.setSoTimeout(hc.getParams(), timeoutSocket);
                System.out.println("timeout socket"+timeoutSocket);
                Log.e("Response", response);
                }
            catch(ConnectTimeoutException e){
                System.out.println(e);
                alertDialog = new AlertDialog.Builder(this).create();
                //alertDialog.setTitle("Reset...");
                System.out.println("internet not available");
                alertDialog.setMessage("Low internet connectivity?");
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                       alertDialog.cancel();
                   }
                });
                //alertDialog.setIcon(R.drawable.icon);
                alertDialog.show();
                //alertDialog.cancel();
            }

but it is not working? What could be the problem. Can anyone help me over this? thanks

Upvotes: 0

Views: 772

Answers (3)

ct_rob
ct_rob

Reputation: 521

Make sure you show the Dialog from the UI thread. If you are performing the network operation on a background thread (as you should do) then you need to explictly call the show method on the UI Thread.

There are several ways to access the UI thread from background threads:

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)

Also see the documentaion on Processes and Threads for more information on this: http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

Upvotes: 0

sampathpremarathna
sampathpremarathna

Reputation: 4064

I have used this

            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo data = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if ((wifi != null & data != null) && (wifi.isConnected() | data.isConnected())) {
              //use the connectivity
            } else {

                //show alert that cannot connect to internet
            }

Upvotes: 1

Umesh
Umesh

Reputation: 1609

Insted of you can check network availability.I hope this will helpfull for you.

public static boolean isNetworkAvailable(Context context) {
       Context mContext = context;
       try{
       ConnectivityManager connectivity = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
       if (connectivity == null) {
       } else {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null) {
             for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                   return true;
                }
             }
          }
       }
       }catch(Exception e){
           e.printStackTrace();
       }
       return false;
    }

If it retuns false then you can use dialog.show();

Upvotes: 1

Related Questions