Android如何在继续之前等待代码完成 [英] Android how to wait for code to finish before continuing

查看:133
本文介绍了Android如何在继续之前等待代码完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 hostPhoto()的方法;它基本上将图像上传到网站并检索链接。
然后我有另一种方法将链接发布到网站。

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.

我的问题是...... hostPhoto()需要几秒钟上传和检索链接,
我的程序似乎不等待并继续发布,因此我将链接保留为null,

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,

无论如何我可以让它先获得链接...然后发布?
喜欢某种onComplete?或类似的东西..
i认为我上面的方法可以工作但是通过做Log.i似乎链接在一秒左右后返回到字符串。

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.

更新:这是我的问题的更新进度,我使用AsyncTask作为通知,但Log.i的错误显示urlLink为空...这意味着从hostphoto请求的链接永远不会回来的时间为日志..

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..

更新2:最终工作!问题是hostPhoto()中的线程,有人可以为我提供一个探索,为什么该线程会导致这个?
感谢所有回复的人。

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()执行此操作:

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


推荐答案

你可以在这里使用AsyncTask,

You can use AsyncTask here,

AsyncTask

通过使用它你可以执行代码

By Using that you can execute the code of

hostPhoto()

在doInBackground()中然后执行代码

in doInBackground() and then execute the code of

post(text + onPostExecute()方法中的+ 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);
    }
 }

并且可以使用

 new MyAsyncTask().execute();

这篇关于Android如何在继续之前等待代码完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆