Android 如何等待代码完成后再继续
我有一个名为 hostPhoto()
的方法;它基本上将图像上传到网站并检索链接。 然后我有另一种方法来发布网站链接。
现在我使用这种方法的方式是这样的:
String link = hostPhoto(); //returns a link in string format
post(text+" "+link); // posts the text + a link.
我的问题是... hostPhoto()
需要几秒钟的时间来上传和检索链接, 我的程序似乎不会等待并继续发布,因此我将链接保留为空,
无论如何我可以让它先获取链接...然后发布吗? 像某种 onComplete 吗?或者类似的东西.. 我认为我上面的方法可以工作,但是通过执行 Log.i ,链接似乎在一秒钟左右后返回到字符串。
更新:这是我的问题的更新进度,我按照通知使用 AsyncTask,但 Log.i 的错误显示 urlLink 为空...这意味着从 hostphoto 请求的链接从未及时返回日志。更新
2:终于可以了!问题是 hostPhoto() 中的线程,有人可以向我解释为什么该线程会导致此问题吗? 感谢所有回复的人。
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() 执行以下操作:
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在这里使用 AsyncTask,
AsyncTask
通过使用您可以执行代码 的
doInBackground() 中的
hostPhoto()
代码,然后在 onPostExecute() 方法中执行
post(text+" "+link);
的代码,这将是最佳解决方案你。
您可以按照此模式编写代码
,并可以使用以下命令执行它
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
and the can execute it using
我假设您正在(或应该)使用单独的线程来异步完成此操作。
您需要将
post()
放置在hostPhoto()
完成时调用的回调中。一般来说,我已经用 Android 的 AsyncTask 完成了这个...
这提供了您可以在回调
onPostExecute()
中执行post()
操作。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 whenhostPhoto()
is complete.Generally I've done this with AsyncTask with Android...
This provides you the callback
onPostExecute()
that you can do yourpost()
inside.您可以使用
AsyncTask
,参考 this :编辑:他是asynctask的视频教程 ,或参考此示例示例:点击此处
you can use
AsyncTask
, refer this :EDIT: and he is a video tutorial of asynctask , or refer this sample example : clic here
您可能需要查看 Java 中的回调模式。
You'll probably need to take a look at callback pattern in Java.
对于你的第二个问题:
您调用“link= photo.post(filepath);”它正在一个新线程上运行。
当该方法仍在运行时,链接仍然为空,并且您当前的线程(主线程)继续使用该链接运行(当时为空)
在这种情况下,您需要等待结果,因此让新线程运行该方法,完成后,该线程将要求主线程更新结果(通过一些回调或处理程序),所有这些工作都被 Android AsyncTask
To your second question:
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