简体   繁体   中英

Android Java Upload Image and then wait to finish asynctask

I am working with AndroidStudio (Java) and there I Post data to URL and upload some images. This process is OK. The problem is that the task is async so it finish after verify other stuff. This is the code:

 boolean ImgSubidasOK = true; ... btnSubirFotosSistema.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ... SubeFotografia(parametro1, parametro2); if (ImgSubidasOK){ //doSomething, ACTIVITY 1 } else { //doSomething, ACTIVITY 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")){ //IMAGE UPLOAD SUCCESSFUL } else { ImgSubidasOK = false; //ERROR UPLOAD IMAGE } } }); String URLFinal = "https://URLASUBIRIMAGENES.php"; task.execute(URLFinal); ... } 

So i would like to get the result of processFinish and then end the function SubeFotografia(parametro1, parametro2); . Because i have the issue that there is an error uploading the image and set the boolean ImgSubidasOK = false AFTER finishing SubeFotografia . So i want to finish SubeFotografia(parametro1, parametro2); FIRST, with the processFinish and THEN check if (ImgSubidasOK) .

How can I do that with asynctask?. Any help would be great, thanks.

I solved it. Changinf this line task.execute(URLFinal);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM