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.
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();
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.
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
You'll probably need to take a look at callback pattern in Java.
you can use AsyncTask
, refer this :
EDIT: and he is a video tutorial of asynctask , or refer this sample example : clic here