Android的AsyncTask的PostExecute [英] Android AsyncTask PostExecute

查看:169
本文介绍了Android的AsyncTask的PostExecute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AsyncTask的一个函数内部
如何返回一个值时,异步执行完成,因为我不能在这里使用PostExecute方法

  AsyncTask.execute(新的Runnable(){
            公共无效的run(){}}

继承人的code。
我需要检索SpinnerList值使用EM在主线程上。
但通常我得到每当我要求的数据,因为它没有返回值但空值。

  AsyncTask.execute(新的Runnable(){
            公共无效的run(){
                尝试{
                    字符串FQL =SELECT UID,名字从用户的WHERE is_app_user和UID IN(SELECT UID2从相知WHERE UID1 =我());
                    捆绑参数=新包();                    parameters.putString(查询,FQL);
                    parameters.putString(法,fql.query);
                    parameters.putString(的access_token,fb.getAccessToken());
                    串响应= fb.request(参数);
                    JSONArray JSON =新JSONArray(响应);
                    名单=新的ArrayList<的String [] []>();
                    的String [] [] = friendsToList新的String [1] [2];
                    SpinnerList =新的ArrayList<串GT;();
                    字符串TempToSpinnerList =新的String();
                    的for(int i = 0; I< json.length(); ++ I){
                        friendsToList [0] [0] = json.getJSONObject(ⅰ).getString(UID);
                        friendsToList [0] [1] = json.getJSONObject(ⅰ).getString(名称);
                        TempToSpinnerList = friendsToList [0] [1];
                            list.add(friendsToList);
                            SpinnerList.add(TempToSpinnerList);
                            Log.e(测试,+ friendsToList [0] [1] ++ friendsToList [0] [0]);
                        }
                        //dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        //spinner.setAdapter(dataAdapter);    / * jsonUser = fb.request(参数);
        friendObj = Util.parseJson(jsonUser);        friendArray = friendObj.getJSONArray(数据);        //
        好友列表=新的ArrayList<的String [] []>();
        的String [] [] = friendsToList新的String [1] [2];
        INT friendCount = 0;
        串FID,FNM;
        的JSONObject的朋友;
        的for(int i = 0; I< friendArray.length();我++){
            //从JSONArray获取一个JSONObject
            朋友= friendArray.getJSONObject(ⅰ);
            //摘录的JSONObject琴弦
           friendsToList [0] [0] = friend.getString(ID);
            friendsToList [0] [1] = friend.getString(名称);
            friendlist.add(friendsToList);
            //设置的值,以我们的数组
         Log.e(测试,+ friendsToList [0] [0] ++ friendsToList [0] [1]);
            friendCount ++;} * /
            }
                赶上(MalformedURLException的E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(IOException异常五){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(FacebookError E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }赶上(JSONException E){
                    // TODO自动生成catch块
                    e.printStackTrace();
                }
            }});


解决方案

如果您要执行一些功能,一旦AsyncTask的完成,你可以设置在调用活动的回调函数。中创建的AsyncTask类的构造函数,并通过它调用的活动。然后用这个引用调用回调函数时,你的任务就完成了。

您也可以从内部onPostExecute,这是简单的,如果你的作品更新UI。

 公共类MyTask扩展的AsyncTask<弦乐,太虚,字符串>
{
    私人MainActivity活动;    公共MyTask(MainActivity活动){
        this.activity =活动;
    }
    @覆盖
    保护无效doInBackground(字符串... PARAMS){
        //做背景的东西
    }    保护无效onPostExecute(){
        //如果需要解雇进度对话框
        //在MainActivity回调函数来表示任务完成
        activity.taskDone(一些字符串);
    }
}

MainActivity.java

  //把你的参数数组和当前活动的AsyncTask的
新MyTask(MainActivity.this).execute(PARAMS);//回调其建成后的AsyncTask调用
公共无效taskDone(字符串returnVal){
    //做的东西一旦数据被加载
    returnText = returnVal;
}

i use AsyncTask inside a function How to return a value when Async Finish Execution , since i cant use PostExecute method here

AsyncTask.execute(new Runnable() {
            public void run() {}}

Heres The Code. I need to retrieve values from SpinnerList to use em on the main thread. But usually i get null values whenever i ask for data , because it didnt return the values yet.

AsyncTask.execute(new Runnable() {
            public void run() {
                try{
                    String fql = "SELECT uid, name FROM user WHERE is_app_user  AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
                    Bundle parameters = new Bundle();

                    parameters.putString("query", fql);
                    parameters.putString("method", "fql.query");
                    parameters.putString("access_token",  fb.getAccessToken());
                    String Response = fb.request(parameters);
                    JSONArray json = new JSONArray(Response);
                    list = new ArrayList<String[][]>();
                    String[][] friendsToList = new String[1][2];
                    SpinnerList = new ArrayList<String>();
                    String TempToSpinnerList=new String();
                    for(int i = 0; i < json.length(); ++i){
                        friendsToList[0][0] = json.getJSONObject(i).getString("uid");
                        friendsToList[0][1] = json.getJSONObject(i).getString("name");
                        TempToSpinnerList= friendsToList[0][1];
                            list.add(friendsToList);
                            SpinnerList.add(TempToSpinnerList);
                            Log.e("Test"," "+friendsToList[0][1]+" "+friendsToList[0][0]);
                        }


                        //dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        //spinner.setAdapter(dataAdapter);



    /*  jsonUser = fb.request(parameters);
        friendObj = Util.parseJson(jsonUser);

        friendArray = friendObj.getJSONArray("data");

        //
        friendlist = new ArrayList<String[][]>();
        String[][] friendsToList = new String[1][2];
        int friendCount = 0;
        String fId, fNm;
        JSONObject friend;
        for (int i = 0;i<friendArray.length();i++){
            //Get a JSONObject from the JSONArray
            friend = friendArray.getJSONObject(i);
            //Extract the strings from the JSONObject
           friendsToList[0][0] = friend.getString("id");
            friendsToList[0][1] = friend.getString("name");
            friendlist.add(friendsToList);
            //Set the values to our arrays
         Log.e("Tester",""+ friendsToList[0][0]+" "+ friendsToList[0][1]);
            friendCount ++;}*/
            }
                catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }});

解决方案

If you want to perform some function once the Asynctask is complete, you can setup a callback function in the calling activity. Create a constructor in the AsyncTask class and pass it the calling activity. Then use this reference to call the callback function when your task is complete.

You can also update the UI from within onPostExecute, which is simpler if it works for you.

public class MyTask extends AsyncTask<String, Void, String>
{
    private MainActivity activity;

    public MyTask(MainActivity activity) {
        this.activity = activity;
    }
    @Override
    protected Void doInBackground(String... params) {
        //Do background stuff
    }

    protected void onPostExecute() {
        //dismiss progress dialog if needed
        //Callback function in MainActivity to indicate task is done
        activity.taskDone("some string");
    }
}

MainActivity.java

//Pass your params array and the current activity to the AsyncTask
new MyTask(MainActivity.this).execute(params);

//Callback for AsyncTask to call when its completed
public void taskDone(String returnVal) {
    //Do stuff once data has been loaded
    returnText = returnVal;
}

这篇关于Android的AsyncTask的PostExecute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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