Android, how to wait for code to complete before continuing

I have a method called hostPhoto(); he basically uploads the image to the site and gets the link. Then I have another way to post a link to a website.

Now Im 'way using this method is as follows:

String link = hostPhoto(); //returns a link in string format

post(text+" "+link); // posts the text + a link.

My problem is that it hostPhoto()takes a few seconds to download and receive the link, my program does not seem to wait and continue publishing, so I left the link as null.

Anyway, can I get him to get the link first ... and then send a message? like some kind of onComplete? or something like that .. I thought my method above would work, but by doing Log.i it seems that the link is returning to the string in a second or so.

UPDATE: this is an update progress of my problem, im using AsyncTask as indicated, but Log.i error showing urlLink as null ... this means the link requested from hostphoto never returned intime for Logs ..

UPDATE 2: FINALLY WORKS! The problem was in the stream in hostPhoto (), can someone give me an explanation why this stream would cause this? Thanks to all who responded.

private class myAsyncTask extends AsyncTask<Void, Void, Void> {
    String urlLink;
    String text;
    public myAsyncTask(String txt){

        text=txt;
    }

    @Override
    protected Void doInBackground(Void... params) {
        urlLink=hostPhoto();
        //Log.i("Linked", urlLink);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        try {
            Log.i("Adding to status", urlLink);
            mLin.updateStatus(text+" "+urlLink);
            Log.i("Status:", urlLink);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

hostPhoto () does the following:

            String link;  new Thread(){

                @Override
                public void run(){
                    HostPhoto photo = new HostPhoto(); //create the host class


                    link= photo.post(filepath); // upload the photo and return the link
                    Log.i("link:",link);
                }
            }.start();
+5
source share
5 answers

Here you can use AsyncTask,

Asynctask

Using this, you can execute code

hostPhoto()

in doInBackground () and then execute the code

post(text+" "+link);

in the onPostExecute () method, which will be the best solution for you.

You can write the code in this template.

private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected Void doInBackground(Void... params) {
        hostPhoto();
        return null;
    }
   @Override
   protected void onPostExecute(Void result) {
        post(text+" "+link);
    }
 }

 new MyAsyncTask().execute();
+8

, ( ) .

post() , , hostPhoto() .

, AsyncTask Android...

onPostExecute(), post() .

+2

AsyncTask, . this:

: : clic here

0

, , Java.

0

:

- , ?

"link = photo.post( );" . , - , ( ) ( )

In this situation, you need to wait for the result, so let the new Thread start this method, and after the end of this thread, the main thread will be requested to update the result (some Callback or Handler), all these tasks are well encapsulated Android AsyncTask

0
source

All Articles