Android Java Wait for an asynchronous process to finish [duplicated]

0

What happens is that I have working on AndroidStudio, programming in Java where you have a function that makes data to a URL that I have, and upload some images, along with other data.

This process is all OK, what happens is that I have the problem that as the process is asynchronous it is finished after checking if all the images are uploaded.

What I have is the following code:

boolean ImgSubidasOK = true;
...

btnSubirFotosSistema.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        ...
          SubeFotografia(parametro1, parametro2);
          if (ImgSubidasOK){
             //hacerAlgo, ACTIVIDAD 1
          }
          else {
              //hacerAlgo, ACTIVIDAD 2
               }
        ...
        }
  }
 ...

public void SubeFotografia(final String parametro1, final String parametro2) {
...

PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
    if (s.contains("uploaded_success")){
        //Subida exitosa imagen
    }
    else {
         ImgSubidasOK = false;
         //Error subida foto
         }
  }
});
String URLFinal = "https://URLASUBIRIMAGENES.php";
task.execute(URLFinal);

 ...

 }

Then I would like that after running SubeFotografia (parametro1, parametro2); already have the final result of processFinish .

Because I have been given the case that there is a data upload error and it goes straight to if (ImgSubidasOK) and does ACTIVITY 1 as if everything had been fine, but really should enter < strong> else and do ACTIVITY 2.

This is because it first enters the if doing ACTIVITY 1 (because that value is true) and then a photo upload error occurs in the processFinish changing the value of ImgSubidasOK to false, but it's too late to verify.

And I need to finish first the SubeFotografia function, along with its asynchronous process, and AFTER to be able to continue with the code that follows (the verifications of if and else ImgSubidasOK)

The code simplifies it to show it here, but has more details that are not relevant. I would appreciate if someone could guide me how to solve that problem please.

    
asked by Pato 05.02.2018 в 16:30
source

2 answers

0

You can not assume that the value of ImgSubidasOK will change when you wait to read it since you are working with asynchronous data. I would recommend changing your code something like this:

public class MyActivity extends AppCompatActivity implements AsyncResponse
    btnSubirFotosSistema.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        ...
            SubeFotografia(parametro1, parametro2, this);



    @Override
    public void processFinish(String output) {
        if (s.contains("uploaded_success")){
            //Subida exitosa imagen, hacer algo Actividad 1
        }
     else {
          //ImgSubidasOK = false; //comentado
          //Error subida foto, actividad 2 hacer algo
     }
    }



....
public void SubeFotografia(final String parametro1, final String parametro2, AsyncResponse asyncResponse) {
    PostResponseAsyncTask task = new PostResponseAsyncTask(MainActivity.this, postData, asyncResponse);

So, the answer is handled in the Activity until the request has really ended (good or bad) and you do not depend on reading a flag.

    
answered by 05.02.2018 в 17:22
0

I solved it. Changing this line: task.execute (Final URL);

For this: String str_result = task.execute (URLFinal) .get ();

    
answered by 06.02.2018 в 16:47