gpt4 book ai didi

android - 如果在 30 秒内未在 android 中从服务器获得响应,如何打开警报对话框?

转载 作者:搜寻专家 更新时间:2023-11-01 08:07:54 25 4
gpt4 key购买 nike

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

}

你好 friend ,如果 30 秒内没有收到服务器响应,我想打开警报。我怎样才能在 asyncTask 中做到这一点。我已经检查了互联网连接,但我也想检查响应..所以请任何人告诉我这怎么可能..谢谢..

最佳答案

试试这个。

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

关于android - 如果在 30 秒内未在 android 中从服务器获得响应,如何打开警报对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com