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 でそれを行うにはどうすればよいですか。私はすでにインターネット接続を確認しましたが、応答も確認したい..どうすればそれが可能か教えてください..ありがとう..

4

1 に答える 1

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 Exception をキャッチする必要があります。

catch (ConnectTimeoutException e)
{
   e.printStackTrace();
   Log.i("==== Connection Timeout","===");
   // Close the Dialog
   throw e;
}
于 2012-10-06T09:49:09.967 に答える