I've tried to implement a asynctask in another asynctask, but it got stuck. When I clicked the buttun to trigger the first asynctask, the second asynctask in the first one does not execute when the first is completed and when I tried to click the button again, the button gave no response.
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);
}
}
}
I implemented the second asynctask in the onPostExecute of the first one. Can anyone point out where I got wrong?
Update:
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);
}
The message showed what I expected. When I commented out the second asynctask, the button still could not trigger again. When I ommented out MainActivity.user_ID = result; and related stuff, the button could trigger again.
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();
}
}
Actually, I want to finish the login page and go back to MainActivity after getting the user ID from Asynctask. But, it stopped unfortunately when the user login successfully.
New post: android - finish the current activity only after the asynctask finishes