为了账号安全,请及时绑定邮箱和手机立即绑定

如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类?

如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类?

一只斗牛犬 2019-05-27 16:47:11
如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类?我有这两节课。我的主要活动和扩展的一个AsyncTask,现在在我的主要活动,我需要从得到的结果OnPostExecute()中AsyncTask。我怎样才能将结果传递给我的主要活动?这是示例代码。我的主要活动。public class MainActivity extends Activity{     AasyncTask asyncTask = new AasyncTask();     @Override     public void onCreate(Bundle aBundle) {         super.onCreate(aBundle);                     //Calling the AsyncTask class to start to execute.           asyncTask.execute(a.targetServer);          //Creating a TextView.         TextView displayUI = asyncTask.dataDisplay;         displayUI = new TextView(this);         this.setContentView(tTextView);      }}这是AsyncTask类public class AasyncTask extends AsyncTask<String, Void, String> {TextView dataDisplay;  //store the data  String soapAction = "   //SOAPAction header line. String targetServer = "https://sampletargeturl.com";   //Target Server.//SOAP Request.String soapRequest = "<sample XML request>";       @Overrideprotected String doInBackground(String... string) {String responseStorage = null; //storage of the responsetry {     //Uses URL and HttpURLConnection for server connection.      URL targetURL = new URL(targetServer);     HttpURLConnection httpCon = (HttpURLConnection) targetURL.openConnection();     httpCon.setDoOutput(true);     httpCon.setDoInput(true);     httpCon.setUseCaches(false);      httpCon.setChunkedStreamingMode(0);     //properties of SOAPAction header     httpCon.addRequestProperty("SOAPAction", soapAction);     httpCon.addRequestProperty("Content-Type", "text/xml; charset=utf-8");      httpCon.addRequestProperty("Content-Length", "" + soapRequest.length());     httpCon.setRequestMethod(HttpPost.METHOD_NAME);     //sending request to the server.     OutputStream outputStream = httpCon.getOutputStream();      Writer writer = new OutputStreamWriter(outputStream);     writer.write(soapRequest);     writer.flush();     writer.close();
查看完整描述

4 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

有几个选择:

  • AsyncTask班级嵌入你的Activity班级。假设您在多个活动中不使用相同的任务,这是最简单的方法。您的所有代码都保持不变,您只需将现有任务类移动到活动类中的嵌套类。

    public class MyActivity extends Activity {
        // existing Activity code
        ...
    
        private class MyAsyncTask extends AsyncTask<String, Void, String> {
            // existing AsyncTask code
            ...
        }}
  • 为您创建一个自定义构造函数,AsyncTask引用您的Activity。您可以使用类似的方式实例化任务new MyAsyncTask(this).execute(param1, param2)

    public class MyAsyncTask extends AsyncTask<String, Void, String> {
        private Activity activity;
    
        public MyAsyncTask(Activity activity) {
            this.activity = activity;
        }
    
        // existing AsyncTask code
        ...}


查看完整回答
反对 回复 2019-05-27
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

我觉得下面的方法非常简单。

我已经声明了一个回调接口

public interface AsyncResponse {
    void processFinish(Object output);}

然后创建异步任务以响应所有类型的并行请求

 public class MyAsyncTask extends AsyncTask<Object, Object, Object> {

    public AsyncResponse delegate = null;//Call back interface

    public MyAsyncTask(AsyncResponse asyncResponse) {
        delegate = asyncResponse;//Assigning call back interfacethrough constructor
    }

    @Override
    protected Object doInBackground(Object... params) {

      //My Background tasks are written here

      return {resutl Object}

    }

    @Override
    protected void onPostExecute(Object result) {
        delegate.processFinish(result);
    }}

然后单击活动类中的按钮时调用异步任务。

public class MainActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {

    Button mbtnPress = (Button) findViewById(R.id.btnPress);

    mbtnPress.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                MyAsyncTask asyncTask =new MyAsyncTask(new AsyncResponse() {

                    @Override
                    public void processFinish(Object output) {
                        Log.d("Response From Asynchronous task:", (String) output);

                        mbtnPress.setText((String) output);
                   }
                });

                asyncTask.execute(new Object[] { "Your request to aynchronous task class is giving here.." });


            }
        });

    }}

谢谢


查看完整回答
反对 回复 2019-05-27
  • 4 回答
  • 0 关注
  • 720 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信