AsyncTask onPostExecute侦听器

时间:2015-12-19 13:17:10

标签: java android android-asynctask

Activity.java

//Activity stuff
MyClass mc = new MyClass();
mc.getText();

public void dosomething() {
    textview.setText(mc.getText());
}

MyClass.java

class MyClass { 
    String text;
    public void setText() {
        class GetTextFromWEB extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {
                String url = urls[0];
                String output;
                //Getting text from web
                return output;
            }
            @Override
            protected void onPostExecute(String _text) {
                text = _text;
            }
        }
        String url = "google.com";
        //Doing with url something
        new GetText().execute(url);
    }
    public String getText() {return text;}
}

Promblem是 - 在活动中setText做得更快,然后AsyncTask做它的工作。
因此,当setText运行时,它就像setText(null)一样运行 我需要检查活动,asynk结束了,所以我要设置我的文本。 我希望我能解释一下

我甚至不需要完全AsyncTask,我需要jsoup工作,所以如果有另一个线程类的解决方案,jsoup将起作用,我可以使用它

修改

class GetLyrics extends AsyncTask<String, Void, String> { //Class for getting lyrics
    private Context con;

    public GetLyrics(Context con) {
        this.con = con;
    }

    @Override
    protected String doInBackground(String... urls) {
        //do something
    }
    @Override
    protected void onPostExecute(String _lyrics) {
        lyrics = _lyrics;
        con.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ((TextView) findViewById(R.id.lyricsOutput)).setText(lyrics);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

1:从我之前的帖子中创建我的第一个项目并在其中添加一些新行以从http:api&#39获取数据。

public class Example extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("parameter1", "xyz"));
        params.add(new BasicNameValuePair("parameter2", "abc"));
        params.add(new BasicNameValuePair("parameter3", "opqr"));
        ServerConnection task = new ServerConnection(this, new ResultListener() {
            @Override
            public void result(String response) {
                Toast.make(this, response, Toast.LENGTH_LONG).show();
            }

            @Override
            public void loader(boolean visble) {
            }

            @Override
            public void connectionLost(String error) {
                Toast.make(this, error, Toast.LENGTH_LONG).show();
            }
        });
    }


    public class ServerConnection extends AsyncTask<String, String, String> implements Constant {
        ResultListener listener;
        private String Method = "GET";
        private List<NameValuePair> params = new ArrayList<NameValuePair>();

        private Context context;
        private ConnectionDetector cd;

        // public static Drawable drawable;
        public ServerConnection(Context context, ResultListener r) {
            this.context = context;
            this.listener = r;
            cd = new ConnectionDetector(context);
            this.execute();
        }

        public boolean isConnection() {
            return cd.isConnectingToInternet();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... arg0) {
            if (!isConnection()) {
                cancel(true);
                return "Sorry!connection lost,try again or later";
            }
            ApiResponse air = new ApiResponse();
            System.out.println("working hre" + "hi");
            String json;

            try {
                json = air.makeHttpRequest(URL, getMethod(), getParams());
            } catch (Exception e) {
                json = e.getMessage();
                cancel(true);
                return json;
            }

            return json;
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        protected void onCancelled(String result) {
            listener.connectionLost(result);
            rl.connectionLost("Sorry!connection lost,try again or later");
            super.onCancelled(result);
        }

        @Override
        protected void onPostExecute(String result) {
            System.out.println("onpost" + result);
            listener.result(result);
            listener.loader(true);
            super.onPostExecute(result);
        }

        public String getMethod() {
            return Method;
        }

        public void setMethod(String method) {
            Method = method;
        }

        public List<NameValuePair> getParams() {
            return params;
        }

        public void setParams(List<NameValuePair> params) {
            this.params = params;
        }
    }         

Example

答案 1 :(得分:0)

调用方法在AsyncTask中的postExecute中设置文本,或直接在postExecute方法上设置文本。

用runOnUIThread中的setText()包装行(否则你会得到一个异常,说只有创建它的线程才能访问该视图,因为你是从异步任务设置文本的。)

设置文本将是这样的

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            ((TextView) findViewById(R.id.txtFieldName)).setText("your text");
        }
    });

这样你可以放弃担心检查异步任务是否完成。但是要避免像这样做复杂的ui操作。因为这只是在TextView上设置文本,所以应该没问题。