问题
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);
}
}
I got the result right in the "onPost passed" log. From here, how can I put the result into TextView in MainActivity?
回答1:
You need to use interface
1.) Create interface class in your asyncTask class.
public interface AsyncResponse {
void processFinish(String output);
}
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.) 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);
}
}
And activity code
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);
}
}
来源:https://stackoverflow.com/questions/37052412/how-to-put-result-from-asynctask-doinbackground-from-different-class-to-textv