[英]How to create a progress dialog inside of AsyncTask?
I have a FullRest API which returns a StatusCode, and I succeed, the problem is, the progress dialog cannot be called from AsyncTask, I just want to "show" the user what's happened, if(statusCode == 201)
.. Successful POST, so far : 我有一个返回状态代码的FullRest API,但我成功了,问题是,无法从AsyncTask调用进度对话框,我只想向用户“显示”发生了什么事情, if(statusCode == 201)
..成功的POST , 至今 :
PostBaseClass.. PostBaseClass ..
public class PostBase {
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private String Url="http://192.168.0.101:3000/";
OkHttpClient client = new OkHttpClient();
int POST(final PostModel model) throws IOException{
Gson gson = new Gson();
String modelJson = gson.toJson(model);
RequestBody body = RequestBody.create(JSON,modelJson);
Request request = new Request.Builder()
.url(Url + "api/gone/POST")
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.code();
}
MainActivity.. 主要活动..
public void MakePost(final PostModel postModel){
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
try {
PostBase postBase = new PostBase();
statusCode = postBase.POST(postModel);
if(statusCode == 201){
//TODO
}else {
//TODO
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
https://developer.android.com/reference/android/os/AsyncTask.html https://developer.android.com/reference/android/os/AsyncTask.html
onPreExecute
and onPostExecute
run on the ui thread while you cannot update ui from doInBackground
as it runs on a background thread onPreExecute
和onPostExecute
在ui线程上运行,而您无法从doInBackground
更新ui,因为它在后台线程上运行
So display dialog in onPreExecute
and dismiss dialog in onPostExecute
因此,在显示的对话框onPreExecute
和解雇对话框onPostExecute
Return some value in doInBackground
and do what is necessary in onPostExecute
based on the value returned. 在doInBackground
返回一些值,并根据返回的值在doInBackground
执行必要的onPostExecute
。
If you want to show progress of the upload you can do that using onProgressUpdate
如果您想显示上传进度,可以使用onProgressUpdate
new AsyncTask<Void,Void,String>(){
@Override
protected void onPreExecute()
{
super.onPreExecute()
// runs on the ui thread
// show dialog
}
@Override
protected String doInBackground(Void... params) {
try {
PostBase postBase = new PostBase();
statusCode = postBase.POST(postModel);
if(statusCode == 201){
return "successful";
}else {
return "notsuccessful"
}
} catch (IOException e) {
e.printStackTrace();
}
return "something";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute();
// dismiss dialog
if(result.equals("successful")
{
// do something on successful
// runs on the ui thread so do update ui
}
}
}.execute();
Apart from the asynctask code it looks like you are using Retrofit
. 除了asynctask代码外,您似乎正在使用Retrofit
。 You can also make asynchronous calls with the same. 您也可以使用相同的方式进行异步调用。
Place your ProgressDialog in onPreExecute, sample code below: 将您的ProgressDialog放在onPreExecute(下面的示例代码)中:
private ProgressDialog pd;
@Override
protected void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(yourContext);
pd.setMessage("Loading...");
pd.show();
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
pd.dismiss();
}
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.