我试图在另一个asynctask中实现asynctask,但它被卡住了。当我点击buttun来触发第一个asynctask时,第一个asynctask中的第二个asynctask在第一个完成时没有执行,当我再次尝试再次单击该按钮时,按钮没有响应。
的java:
public class BackgroundTask extends AsyncTask<String, Void, String>{
String task = "";
Context context;
String user_name = "";
String user_ac = "";
String user_pw = "";
BackgroundTask (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String register_url = "http://www.minigameserver.square7.ch/register.php";
String login_url = "http://www.minigameserver.square7.ch/login.php";
String get_userID_url = "http://www.minigameserver.square7.ch/get_userID.php";
String task_url = "";
task = params[0];
if(task.equals("register")){
task_url = register_url;
user_name = params[1];
user_ac = params[2];
user_pw = params[3];
}
if(task.equals("login")){
task_url = login_url;
user_ac = params[1];
user_pw = params[2];
}
if(task.equals("get_userID")){
task_url = get_userID_url;
user_ac = params[1];
}
try {
URL url = new URL(task_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = "";
if(task.equals("register")){
data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
URLEncoder.encode("user_ac", "UTF-8") + "=" + URLEncoder.encode(user_ac, "UTF-8") + "&" +
URLEncoder.encode("user_pw", "UTF-8") + "=" + URLEncoder.encode(user_pw, "UTF-8");
}
if(task.equals("login")){
data = URLEncoder.encode("user_ac", "UTF-8") + "=" + URLEncoder.encode(user_ac, "UTF-8") + "&" +
URLEncoder.encode("user_pw", "UTF-8") + "=" + URLEncoder.encode(user_pw, "UTF-8");
}
if(task.equals("get_userID")){
data = URLEncoder.encode("user_ac", "UTF-8") + "=" + URLEncoder.encode(user_ac, "UTF-8");
}
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
InputStream IS = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS));
StringBuilder result = new StringBuilder();
String getresult = "";
while ((getresult = bufferedReader.readLine()) != null) {
result.append(getresult);
}
bufferedReader.close();
IS.close();
String result_newline = result.toString();
result_newline = result_newline.replace("\\n", System.getProperty("line.separator"));
return result_newline;
}
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "fail!";
}
@Override
protected void onPostExecute(String result) {
super.onPreExecute();
if(task.equals("get_userID")){
MainActivity.user_ID = result;
Toast.makeText(context,"GET",Toast.LENGTH_LONG);
}
if(task.equals("register") || task.equals("login")){
//Toast.makeText(context,result,Toast.LENGTH_LONG).show();
BackgroundTask get_userID_task = new BackgroundTask(context);
get_userID_task.execute("get_userID",user_ac);
}
}
}
我在第一个onPostExecute中实现了第二个asynctask。任何人都可以指出我错在哪里吗?
更新
if(task.equals("register") || task.equals("login")){
MainActivity.user_ID = result;
Toast.makeText(context,MainActivity.user_ID,Toast.LENGTH_LONG).show();
//BackgroundTask get_userID_task = new BackgroundTask(context);
//get_userID_task.execute("get_userID",user_ac);
}
消息显示了我的预期。 当我注释掉第二个asynctask时,按钮仍然无法再次触发。当我注意到MainActivity.user_ID = result;和相关的东西,按钮可以再次触发。
UPDATE2:
的java:
public interface AsyncResponse {
void processFinish();
}
public class BackgroundTask extends AsyncTask<String, Void, String>{
AsyncResponse delegate = null;
...
protected void onPostExecute(String result) {
...
delegate.processFinish();
}
}
// MainActivity has a button to go to login
public class login extends AppCompatActivity implements AsyncResponse{
...
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.delegate = this;
backgroundTask.execute("login",user_ac_s,user_pw_s);
public void processFinish(){
if(!MainActivity.user_ID.equals("0"))
finish();
}
}
实际上,我想完成登录页面并在从Asynctask获取用户ID后返回MainActivity。但是,当用户成功登录时,它停止了。
新帖子: android - finish the current activity only after the asynctask finishes