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();
post(text+" "+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();
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) {
e.printStackTrace();
}
}
}
hostPhoto () does the following:
String link; new Thread(){
@Override
public void run(){
HostPhoto photo = new HostPhoto();
link= photo.post(filepath);
Log.i("link:",link);
}
}.start();