Android:没有显示名为警告对话框的Web服务时的操作超时

时间:2011-12-12 08:42:36

标签: android response

如果互联网连接较低,我想显示一个警告对话框。我正在使用以下代码:

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

但它不起作用? 可能是什么问题呢。 任何人都可以帮助我吗? 感谢

3 个答案:

答案 0 :(得分:1)

您可以检查网络可用性。我希望这对您有所帮助。

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

如果返回false,则可以使用dialog.show();

答案 1 :(得分:1)

我用过这个

            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
            }

答案 2 :(得分:0)

确保从UI线程中显示对话框。如果您正在后台线程上执行网络操作(您应该这样做),那么您需要在UI线程上明确地调用show方法。

有几种方法可以从后台线程访问UI线程:

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

有关详细信息,请参阅有关进程和线程的文档:http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html