11

I have a method called hostPhoto(); it basically uploads an image to a site and retrieves a link. I then have an other method to post the link to a website.

Now the way Im' using this method is like this:

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

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

My problem is... that the hostPhoto() takes a few seconds to upload and retrieve the link, my program seems to not wait and continue posting, therefore im left with the link as null,

Is there anyway i could make it get the link first... and then post? like some sort of onComplete? or something like that.. i thought my method above would work but by doing Log.i's it seems the link is returned to the string after a second or so.

UPDATE: This is the updates progress on my problem, im using a AsyncTask as informed, but the Log.i's error out showing the urlLink as a null... this means that the link requested from hostphoto never came back intime for the Logs..

UPDATE 2: FINALLY WORKS! The problem was the Thread within the hostPhoto(), could someone provide me an explination why that thread would have caused this? Thanks to all who replied.

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 this:

            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();
Houcine
  • 24,001
  • 13
  • 56
  • 83
asd2005
  • 315
  • 2
  • 5
  • 12
  • You should post more code, especially the contents of `hostPhoto()`, as the behaviour you're describing is non-standard. – Alex Aug 22 '11 at 15:43
  • An `AsyncTask` does not solve this issue since `hostPhoto()` doesn't even block the thread. Like atc mentioned, you need to post code from `hostPhoto()`. – Che Jami Aug 22 '11 at 17:13
  • the code of hostPhoto has been updated – asd2005 Aug 22 '11 at 18:56
  • UPDATE 2: FINALLY WORKS! The problem was the Thread within the hostPhoto(), could someone provide me an explination why that thread would have caused this? Thanks to all who replied. – asd2005 Aug 22 '11 at 20:05
  • Since AsyncTask is now deprecated, what should we use now? – Ieshaan Saxena Jan 11 '22 at 10:32

5 Answers5

14

You can use AsyncTask here,

AsyncTask

By Using that you can execute the code of

hostPhoto()

in doInBackground() and then execute the code of

post(text+" "+link);

in the onPostExecute() Method, that will the best solution for you.

You can write the code in this pattern

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);
    }
 }

and the can execute it using

 new MyAsyncTask().execute();
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Hey, thank you for your reply. After carrying out your method im getting an error.Check out hte updated question above – asd2005 Aug 22 '11 at 18:45
  • FINALLY WORKS! The problem was the Thread within the hostPhoto(), could someone provide me an explination why that thread would have caused this? – asd2005 Aug 22 '11 at 20:06
2

I'm assuming you are (or should be) using a separate thread to asynchronously accomplish this action.

You need to place the post() in a callback that is called when hostPhoto() is complete.

Generally I've done this with AsyncTask with Android...

This provides you the callback onPostExecute() that you can do your post() inside.

jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
0

To your second question:

could someone provide me an explanation why that thread would have caused this?

You call "link= photo.post(filepath);" which is running on a new thread. While that method is still running, link is still null, and your current thread (main thread) continues to run with that link(null at that time)

In this situation you need to wait for the result, so let the new Thread run the method, and after completing, that thread will ask the main thread to update result(by some Callback or Handler), all of those jobs are well encapsulated by Android AsyncTask

Tin Luu
  • 1,557
  • 16
  • 22
0

you can use AsyncTask , refer this :

EDIT: and he is a video tutorial of asynctask , or refer this sample example : clic here

Houcine
  • 24,001
  • 13
  • 56
  • 83
0

You'll probably need to take a look at callback pattern in Java.

Ashkan Aryan
  • 3,504
  • 4
  • 30
  • 44