gpt4 book ai didi

android - 停止不确定的进度条android

转载 作者:行者123 更新时间:2023-11-29 17:09:18 24 4
gpt4 key购买 nike

我的 android 应用程序使用 ProgressDialog。当向服务器(RESTFul Webservice)发送 http 请求时显示对话框,当从服务器接收结果时隐藏对话框

private class GetMyTimeSheetTask extends AsyncTask<String, Void, String>
{
private ProgressDialog progress;

protected String doInBackground(String... params) {

MyTimeSheetFragment.this.getActivity().runOnUiThread(new Runnable() {
public void run() {
progress = new ProgressDialog(MyTimeSheetFragment.this.getContext());
progress.setTitle(getResources().getString(R.string.app_name));
progress.setMessage(getResources().getString(R.string.loading));
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(false);
progress.show();
}
} );

String result = HttpUtil.httpGet(
params[0], params[1],
params[2], params[3],
params[4]);
return result;
}

protected void onPostExecute(String param) {
progress.dismiss();
....
}
}

问题是有时服务器有问题,进度对话框一直显示,我不知道如何关闭它。如何解决这个问题呢?感谢您的帮助。

最佳答案

您的 HttpUtil 应该在发生错误时返回错误消息,并在超时时返回超时信息,因此您可以在收到这些异常消息时隐藏对话框。我看不到 HttpUtil 代码,这里可能是目前的解决方案:

private class GetMyTimeSheetTask extends AsyncTask<String, Void, String> {  
private static final int REQUEST_TIMEOUT = 5000;
private ProgressDialog progress;
private Handler handler = new Handler(Looper.getMainLooper());
private Runnable hideDialogTask = new Runnable(){
@Override
public void run() {
progress.dismiss();
}
};

protected String doInBackground(String... params) {

MyTimeSheetFragment.this.getActivity().runOnUiThread(new Runnable(){
public void run() {
progress = new ProgressDialog(MyTimeSheetFragment.this.getContext());
progress.setTitle(getResources().getString(R.string.app_name));
progress.setMessage(getResources().getString(R.string.loading));
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(false);
progress.show();
handler.postDelayed(hideDialogTask, REQUEST_TIMEOUT);
}
} );

String result = HttpUtil.httpGet(
params[0], params[1],
params[2], params[3],
params[4]);
return result;
}

protected void onPostExecute(String param) {
progress.dismiss();
....
}
}

关于android - 停止不确定的进度条android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40879461/

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