2012-10-06 17 views
2
public class AllDataAsyn extends AsyncTask<String, Void, Void> { 

    @Override 
    protected Void doInBackground(String... params) { 
     // TODO Auto-generated method stub 

     getData(params[0].toString()); 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result);   


     if (isNetworkConnected() == true) { 

      progressDialog.dismiss();   
      adapter = new NewsScreenAdapter(NewsScreenActivity.this); 
      list.setAdapter(adapter); 

      } else if(isNetworkConnected() == false) { 

      // runDialog(seconds);  
       AlertDialog connection = new AlertDialog.Builder(
         NewsScreenActivity.this) 
         .setTitle("No Network Found") 
         .setMessage(
           "Internet Connection Reqired To Use this Application") 
         .setPositiveButton("Ok", 
           new DialogInterface.OnClickListener() { 

            public void onClick(DialogInterface dialog, 
              int whichButton) { 

             progressDialog.dismiss(); 
            } 
           }).create(); 

       connection.show(); 
      }  
     } 


    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 

     progressDialog = new ProgressDialog(NewsScreenActivity.this); 
      progressDialog.setMessage("Loding ..."); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 

    } 

    public boolean isNetworkConnected() { 

     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     if (ni == null) { 
      // There are no active networks. 

      // Toast.makeText(getBaseContext(), 
      // " Internet Connection Reqired To Use this Application", 
      // Toast.LENGTH_SHORT).show(); 
      return false; 
     } else 
      // Toast.makeText(getBaseContext(), " Network Found", 
      // Toast.LENGTH_SHORT).show(); 
      return true; 
    } 

} 

你好朋友,我想打開警報,如果沒有得到從服務器的響應30秒。我如何在asyncTask中做到這一點。我已經檢查互聯網連接,但我想檢查響應也..所以請任何人都可以告訴我怎麼可能..謝謝..如何打開警報對話框,如果沒有得到從服務器在30秒內的響應android中?

回答

4

試試這個。

HttpGet httpGet = new HttpGet(url); 
HttpParams httpParameters = new BasicHttpParams(); 
// Set the timeout in milliseconds until a connection is established. 
// The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000; 
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
// Set the default socket timeout (SO_TIMEOUT) 
// in milliseconds which is the timeout for waiting for data. 
int timeoutSocket = 5000; 
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
HttpResponse response = httpClient.execute(httpGet); 

If you want to set the Parameters of any existing HTTPClient (e.g. DefaultHttpClient or AndroidHttpClient) you can use the function setParams(). 

httpClient.setParams(httpParameters); 

您必須趕上ConnectionTimeout異常。

catch (ConnectTimeoutException e) 
{ 
    e.printStackTrace(); 
    Log.i("==== Connection Timeout","==="); 
    // Close the Dialog 
    throw e; 
} 
+0

如何在HttpPost()中使用? – moDev

相關問題