I am making an app in which I use Asynctack
to fetch login details for me
我正在制作一个应用程序,我使用Asynctack来获取登录详细信息
User enters username and password and I the following function from a different class
用户输入用户名和密码,我从其他类输入以下功能
static int success;
public static boolean authenticate(final String emailId , final String password , final ProgressDialog progressDialog) {
data = new Data();
new AsyncTask<Void , Void, Void>(){
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Authenticating...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... param) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username" , emailId));
params.add(new BasicNameValuePair("password" , password));
JSONObject json = jsonParser.makeHttpRequest(url_login, "POST", params);
try {
success = json.getInt("success");
if (success ==1){
JSONArray student = json.getJSONArray("student");
JSONObject jobject = student.getJSONObject(0);
...loading details...
}
} catch (JSONException e) {
Log.e("Authentication error" , e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
}.execute();
if (success==1){
return true;}
else return false;
}
here I call it
我在这称呼它
ProgressDialog progressDialog = new ProgressDialog(this);
if(Login.authenticate(username,password,progressDialog)) {
Toast.makeText(this, "Authenticated\nWelcome " + Login.data.Fullname, Toast.LENGTH_SHORT).show();
..calling next activity...
...bla bla bla...
}
else Toast.makeText(this, "Authentication Failed" , Toast.LENGTH_SHORT).show();
In this IF condition the ProgressDialog
appears and with that 2nd toast appears i.e. Authentication Failed and nothing happens
在此IF条件下,出现ProgressDialog并显示第二个Toast,即Authentication Failed且没有任何反应
I think I know whats the problem here but cant figure it out
我想我知道这里的问题是什么,但无法弄明白
I think UI thread must wait till the Asynctask
completes its task and then return anything
我认为UI线程必须等到Asynctask完成其任务然后返回任何内容
2 个解决方案
#1
1
I think UI thread must wait till the AsyncTask completes its task and then return anything
我认为UI线程必须等到AsyncTask完成其任务然后返回任何内容
When calling AsyncTask.execute
UI Thread not wait for complete and return from AsyncTask.
调用AsyncTask.execute时,UI线程不等待完成并从AsyncTask返回。
But onPostExecute
is a method which is called on UI Thread when doInBackground
so do all work which want to execute according to AsyncTask result inside onPostExecute
method like want to show Toast message or start a new Activity,...
但是onPostExecute是一个在doBackground上调用UI线程的方法,所以根据onPostExecute方法中的AsyncTask结果执行所有想要执行的工作,比如要显示Toast消息或启动一个新的Activity,...
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (success==1){
// start new Activity here
}
else {
// show fail message
}
}
#2
1
You are going in wrong direction. Why make main thread wait when async task is executing?
你的方向错了。为什么在执行异步任务时让主线程等待?
Instead you can achieve that using following steps
相反,您可以使用以下步骤实现此目的
- Show ProgressDialog
- 显示ProgressDialog
- start async task and let it execute.
- 启动异步任务并让它执行。
- in
onPostExecute()
notify UI to stop the progressDialog - 在onPostExecute()中通知UI停止progressDialog
#1
1
I think UI thread must wait till the AsyncTask completes its task and then return anything
我认为UI线程必须等到AsyncTask完成其任务然后返回任何内容
When calling AsyncTask.execute
UI Thread not wait for complete and return from AsyncTask.
调用AsyncTask.execute时,UI线程不等待完成并从AsyncTask返回。
But onPostExecute
is a method which is called on UI Thread when doInBackground
so do all work which want to execute according to AsyncTask result inside onPostExecute
method like want to show Toast message or start a new Activity,...
但是onPostExecute是一个在doBackground上调用UI线程的方法,所以根据onPostExecute方法中的AsyncTask结果执行所有想要执行的工作,比如要显示Toast消息或启动一个新的Activity,...
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (success==1){
// start new Activity here
}
else {
// show fail message
}
}
#2
1
You are going in wrong direction. Why make main thread wait when async task is executing?
你的方向错了。为什么在执行异步任务时让主线程等待?
Instead you can achieve that using following steps
相反,您可以使用以下步骤实现此目的
- Show ProgressDialog
- 显示ProgressDialog
- start async task and let it execute.
- 启动异步任务并让它执行。
- in
onPostExecute()
notify UI to stop the progressDialog - 在onPostExecute()中通知UI停止progressDialog