2011-12-13 50 views
0

如果互联网网络较低,服务器端响应时间超过1分钟,我需要显示一个对话框。如何完成这项任务。 我正在使用下面的代码。 但它不工作故意.:Android:等待服务器响应一分钟,否则显示警告对话框

try 
{ 
HttpConnectionParams.setConnectionTimeout(hc.getParams(),60000); 
int timeoutSocket = 60*1000; 
    HttpConnectionParams.setSoTimeout(hc.getParams(), timeoutSocket); 
} 


catch(ConnectTimeoutException e){ 
            //System.out.println(e); 
            m_Progress.cancel(); 
            alertDialog = new AlertDialog.Builder(AdminEbooks.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(); 
             } 
            }); 
           } 

回答

1

听起来像是你需要把它在后台线程。我推荐使用AsyncTask

您将需要覆盖onPreExecute(),doInBackground()onPostExecute(),以达到您正在尝试完成的任务。

onPreExecute()和在UI线程上执行,因此您的对话框可以显示在这些方法中。

我推荐doInBackground()返回boolean所以onPostExecute()可以显示正确的对话框。

1

制作方法用于检查响应时间,

public boolean checkURL() { 

    boolean exist = false; 
    try { 
     URL url=new URL("http://................."); 
     HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setConnectTimeout(60000); 
     urlConnection.connect(); 

     exist = true; 
    } catch(Exception e) { 
     e.printStackTrace(); 
     exist = false; 
    } 

    return exist; 
} 

它返回FLASE,如果在60秒

现在执行条件没有响应,

if(chcekURL){ 
} else { 

            alertDialog = new AlertDialog.Builder(AdminEbooks.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(); 
             } 
            }); 
} 
+0

我检查这一点,也没有工作..它只是显示一个黑色的屏幕没有别的:( – ekjyot

+0

哎neetesh其工作,但u能PLZ告诉我的其他因为当我的w/s进度dailog运行,然后我得到两个日志从w/s。1)为我的登录响应和2)形式URL url = new URL(“http:// ....... ..........“);所以我的w/s检查Url两次,并显示日志两次。 – Google

3

这里是我” m这样做:

public void UseHttpConnection(String url, String charset, String query) { 
    try { 
     System.setProperty("http.keepAlive", "false"); 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     connection.setDoOutput(true); 
     connection.setConnectTimeout(15000 /* milliseconds */); 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Charset", charset); 
     connection.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded;charset=" + charset); 
     OutputStream output = null; 
     try { 
      output = connection.getOutputStream(); 
      output.write(query.getBytes(charset)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      showError2("Check your network settings!"); 

     } finally { 
      if (output != null) 
       try { 
        output.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     } 

     int status = ((HttpURLConnection) connection).getResponseCode(); 
     Log.d("", "Status : " + status); 

     for (Entry<String, List<String>> header : connection 
       .getHeaderFields().entrySet()) { 
      Log.d("Headers", 
        "Headers : " + header.getKey() + "=" 
          + header.getValue()); 
     } 

     InputStream response = new BufferedInputStream(connection.getInputStream()); 

     int bytesRead = -1; 
     byte[] buffer = new byte[30 * 1024]; 
     while ((bytesRead = response.read(buffer)) > 0) { 
      byte[] buffer2 = new byte[bytesRead]; 
      System.arraycopy(buffer, 0, buffer2, 0, bytesRead); 
      handleDataFromSync(buffer2); 
     } 
     connection.disconnect(); 
    } catch (FileNotFoundException e) { 

     e.printStackTrace(); 
     showError2("Check your network and server settings!"); 


    } catch (IOException e) { 
     showError2("Check your network settings!"); 
     e.printStackTrace(); 
    } 
} 

基本上,如果你的连接超时,它会抛出你需要捕获的IOException,并在那里创建警报对话框。至少这是我正在做的事情,它的工作。

0

对于我的对话我刚才而是用

String error = e.toString(); 
      Dialog d = new Dialog(this); 
      d.setTitle("Dialog Title"); 
      TextView tv = new TextView(this); 
      tv.setText(error); 
      d.setContentView(tv); 
      d.show();