作者:崔显莉京_716 | 来源:互联网 | 2023-05-26 15:13
这是我的代码:
private void downloadSupplyTownData(final int townId2) {
/*******************
* Using Volley
*******************/
// Post params to be sent to the server
HashMap params = new HashMap();
params.put("ID",townId2);
CustomDialogClass.showProgressDialog(context,true);
JsonObjectRequest req = new JsonObjectRequest(Consts.baseUrl+Consts.townSupplyUrl, new JSONObject(params),
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
totalCOnsumerRecords= Integer.parseInt(response.getString("TotalConsumerRecords").trim());
if(totalConsumerRecords>0)
{
/**For -----**/
JSONArray dtrArray = response.getJSONArray("xxx");
for(int i=0;i
这里显示和隐藏ProgressDialog CustomDialogClass.showProgressDialog(context,true);
进度对话框先旋转2-3秒然后卡住.请帮我解决这个问题.
编辑
/***显示Dialog**/@Override protected Dialog onCreateDialog(int id){switch(id){case progress_bar_type://我们将其设置为0 pDialog = new ProgressDialog(this); pDialog.setMessage("正在下载文件.请稍候..."); pDialog.setIndeterminate(假); pDialog.setMax(100); pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pDialog.setCancelable(真); pDialog.show(); 返回pDialog; 默认值:返回null; }}
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask {
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
downloadSupplyTownData(townId);
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
}
}
从视图中调用AsynchTask new DownloadFileFromURL().execute();
1> ρяσѕρєя K..:
进度对话框先旋转2-3秒然后卡住.
因为解析json响应和downloadConsumerData
方法从主UI线程发出api请求.
在Volley
onResponse
网络请求完成时在主UI线程上称为回调方法的方法中.
用于在onResponse
方法使用AsyncTask
类中解析请求响应并在方法中调用ProgressDialog
dismiss方法,该onPostExecute
方法将ProgressDialog
在Volley请求时关闭,downloadConsumerData
在后台线程中完成json数据和方法作业的解析
从onResponse
方法启动AsyncTask :
@Override
public void onResponse(JSONObject response) {
new DownloadFileFromURL().execute(response);
}
在doInBackground
进程JSON数据中:
@Override
protected String doInBackground(String... f_url) {
totalCOnsumerRecords= Integer.parseInt(response.getString
("TotalConsumerRecords").trim());
// add all code here from onResponse
downloadSupplyTownData(townId);
return null;
}