从AsyncTask获得结果

时间:2014-03-15 20:52:33

标签: android android-asynctask

我试图将AsyncTask的结果传递给另一个类。我正在使用接口来实现这一目标。我在另一个模块中执行相同的过程并且它运行良好但我在这种情况下无法确定问题... 我是从另一个班级这样打来的。

GroupDetails gd=new GroupDetails();
groups=gd.getGroupList("email");




public class GroupDetails implements AsyncResponse {
    String result;
    String[] groupList;

    public String getGroupList(String userEmail){

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("user_email", userEmail));
        PhpConnect phpcon=new PhpConnect("http://www.pinnacle2k14.com/letsmeet/get_group.php",postParameters);
        phpcon.delegate=this;
        phpcon.execute();
        result="hello"
        return result;
    }

    @Override
    public void processFinish(String output) { //this method not functioning why?
        // TODO Auto-generated method stub
        result="hello";
        //groupList=output.split(",");


    }

}

PhpConnect.php

public class PhpConnect extends AsyncTask<String, Void, String> {

    String url1;
    ArrayList<NameValuePair> postParameters1;

    public PhpConnect(String url,ArrayList<NameValuePair> postParameters){
        url1=url;
        postParameters1=postParameters;

    }
    public PhpConnect() {
        // TODO Auto-generated constructor stub
    }
    String response;
    public AsyncResponse delegate=null;

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub

        try {
            response = CustomHttpClient.executeHttpPost(url1,postParameters1);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            response=e.toString();
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        delegate.processFinish(result);
    }


}

AsynResponse界面

public interface AsyncResponse {
    void processFinish(String output);

}

1 个答案:

答案 0 :(得分:0)

你知道你可以做到

PhpConnect phpcon=new PhpConnect("http://www.pinnacle2k14.com/letsmeet/get_group.php",postParameters) {

    @Override
    public void onPostExecute(String result) {

    //DO YOUR STUFF

    }

};

phpcon.execute();

这样您就可以保存回调。