程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-從AsyncTask中獲取數據的問題

java-從AsyncTask中獲取數據的問題

編輯:編程綜合問答
從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 });

    }

}

最佳回答:


修改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(""); 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved