2 hcg 21 hcg_21 于 2012.10.19 11:42 提问

从AsyncTask中获取数据的问题

MainActivity调用 DataCall.getJSON方法,然后触发AsyncTask。但是我不确定如何将数据返回到原始的Activity。如何从AsyncTask中获取数据?
MainActivity调用 DataCall,应该返回一个字符串并将其保存在state_data。

String state_data =  DataCall.getJSON(spinnerURL,spinnerContentType);

DataCall:

public class DataCall extends Activity {
    private static final String TAG = "MyApp";


    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {


        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }


        protected void onPostExecute(String result) {
            //THIS IS WHERE I NEED TO RETURN MY DATA TO THE MAIN ACTIVITY. (I am guessing)
        }

        }

    public void getJSON(String myUrlString, String contentType) {
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.mywebsite.com/" + myUrlString });

    }

}

1个回答

Baby_Bonnie
Baby_Bonnie   2012.10.24 16:35
已采纳

修改AsyncTask:

public class GetData extends AsyncTask<String, Void, String> 
{ 
    DataDownloadListener dataDownloadListener; 
    public GetData() 
    { 

    } 
    public void setDataDownloadListener(DataDownloadListener dataDownloadListener) { 
        this.dataDownloadListener = dataDownloadListener; 
    } 
    @Override 
    protected Object doInBackground(Object... param)  
    { 

        return null; 
    } 
    @Override 
    protected void onPostExecute(Object results) 
    {        
        if(results != null) 
        {                
        dataDownloadListener.dataDownloadedSuccessfully(results); 
        } 
        else 
        dataDownloadListener.dataDownloadFailed(); 
    } 
    public static interface DataDownloadListener { 
        void dataDownloadedSuccessfully(Object data); 
        void dataDownloadFailed(); 
    } 
}

在Activity中使用:

GetData getdata = new GetData(); 
getdata.setDataDownloadListener(new DataDownloadListener() 
{ 
    @SuppressWarnings("unchecked") 
    @Override 
    public void dataDownloadedSuccessfully(Object data) { 

    } 
    @Override 
    public void dataDownloadFailed() { 

    } 
}); 
getdata.execute(""); 
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!