2011-12-13 51 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();