如何将结果从“AsyncTask doInBackground from different class”到“MainActivity中的TextView” [英] How to put result from "AsyncTask doInBackground from different class" to "TextView in MainActivity"

查看:289
本文介绍了如何将结果从“AsyncTask doInBackground from different class”到“MainActivity中的TextView”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的android。这个问题是一个阶段,从我的步骤实验与Android应用程序和Web服务。我之前提出过一个问题,其中包括:无法将Android连接到网络服务
下面是我的更新代码,
我的代码在MainActivity:

I am new to android. This question is a phase from my steps in experimenting with android apps and web service. I have asked a question before, which is in here: Fail to connect android to web service. Below is my Updated code, my code in MainActivity:

public class MainActivity extends AppCompatActivity {
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        Mytask mt = new Mytask();
        mt.execute();


    }

}



my code in Mytask:

public class Mytask extends AsyncTask<String, Integer, String> {

    private static final String SOAP_ACTION = "http://services.aonaware.com/webservices/Define";
    private static final String URL = "http://services.aonaware.com/DictService/DictService.asmx";
    private static final String METHOD_NAME = "Define";
    private static final String NAMESPACE =  "http://services.aonaware.com/webservices/";

    String resultData=null;


    @Override
    protected String doInBackground(String... params) {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo property = new PropertyInfo();
        property.setName("word");
        property.setType(String.class);
        property.setValue("computer");
        request.addProperty(property);
        Log.i("soap tobe", "----");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Object response = null;
        try {
            androidHttpTransport.getServiceConnection();
            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            Log.i("soap passed", "----"+response);
            response =  envelope.getResponse();
            resultData= response.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("result passed", "----"+response.toString());

        return resultData;
    }
    protected void onPostExecute(String result) {
        Log.i("onPost passed", "----"+result);
    }

}

onPost passed日志。从这里,我如何将结果放到MainActivity中的TextView?

I got the result right in the "onPost passed" log. From here, how can I put the result into TextView in MainActivity?

推荐答案

您需要使用interface

You need to use interface

1。)在asyncTask类中创建接口类。

1.) Create interface class in your asyncTask class.

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

2)并将asyncResponse接口声明为asyncTask类中的一个字段: p>

2.) And declare interface AsyncResponse as a field in asyncTask class:

public class MyAsyncTask extends AsyncTask{
  public AsyncResponse delegate = null;
@Override
protected void onPostExecute(String result) {
  delegate.processFinish(result);
}
 }

3。)在您的主Activity中,接口AsyncResponse。

3.) In your main Activity you need to implements interface AsyncResponse.

public class MainActivity implements AsyncResponse{
  MyAsyncTask asyncTask =new MyAsyncTask();



@Override
  public void onCreate(Bundle savedInstanceState) {

 //this to set delegate/listener back to this class
 asyncTask.delegate = (MyAsyncTask)this;

 //execute the async task 
 asyncTask.execute();
  }



//this override the implemented method from asyncTask
  void processFinish(String output){
     //Here you will receive the result fired from async class 
     //of onPostExecute(result) method.
   }
 }

编辑 >

Edit

public class Mytask extends AsyncTask<String, Integer, String> {

    private static final String SOAP_ACTION = "http://services.aonaware.com/webservices/Define";
    private static final String URL = "http://services.aonaware.com/DictService/DictService.asmx";
    private static final String METHOD_NAME = "Define";
    private static final String NAMESPACE =  "http://services.aonaware.com/webservices/";

    String resultData=null;
    public AsyncResponse delegate = null;


    @Override
    protected String doInBackground(String... params) {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        PropertyInfo property = new PropertyInfo();
        property.setName("word");
        property.setType(String.class);
        property.setValue("computer");
        request.addProperty(property);
        Log.i("soap tobe", "----");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Object response = null;
        try {
            androidHttpTransport.getServiceConnection();
            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            Log.i("soap passed", "----"+response);
            response =  envelope.getResponse();
            resultData= response.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("result passed", "----"+response.toString());

        return resultData;
    }

    protected void onPostExecute(String result) {
        Log.i("onPost passed", "----"+result);
        delegate.processFinish(result);

    }

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

}

和活动代码

public class MainActivity extends AppCompatActivity implements AsyncResponse{
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        Mytask mt = new Mytask();
        mt.delegate = this;
        mt.execute();
    }

    void processFinish(String output){
        tv.setText(output); 
    }

}

这篇关于如何将结果从“AsyncTask doInBackground from different class”到“MainActivity中的TextView”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆