Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

java - Return data from AsyncTask class

How do I get the data from my AsyncTask? My MainActivity is calling the DataCall.getJSON function that triggers the AsyncTask but I am not sure how to get the data back to the original Activity.

MainActivity with call to DataCall that should return a string and save it in 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 });

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

modify your AsyncTask as below:

public class GetData extends AsyncTask<String, Void, String>
{
    DataDownloadListener dataDownloadListener;
    public GetData()
    {
        //Constructor may be parametric 
    }
    public void setDataDownloadListener(DataDownloadListener dataDownloadListener) {
        this.dataDownloadListener = dataDownloadListener;
    }
    @Override
    protected Object doInBackground(Object... param) 
    {
        // do your task...
        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();
    }
}

and use it in your Activity

GetData getdata = new GetData();
getdata.setDataDownloadListener(new DataDownloadListener()
{
    @SuppressWarnings("unchecked")
    @Override
    public void dataDownloadedSuccessfully(Object data) {
    // handler result
    }
    @Override
    public void dataDownloadFailed() {
    // handler failure (e.g network not available etc.)
    }
});
getdata.execute("");

NOTE: For the people who are reading this.

Please consider this post for the best and perhaps right implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...