問題描述
我是安卓開發(fā)的新手.我正在嘗試開發(fā)一個(gè)將與 .net webservice 連接以檢索數(shù)據(jù)的應(yīng)用程序.我想使用 AsyncTask
進(jìn)行 ksoap2 調(diào)用.我如何用 asynctask 調(diào)用它asyncronus?
I am a newbie on android development. I am trying to develop an application which will connect with .net webservice in order to retrieve data. I would like to make the ksoap2 call with AsyncTask
. How I call it asyncronus with asynctask?
我的 SoapCall 類是
My SoapCall class is
public class SoapCall {
public final static String SOAP_ACTION = "http://www.alpha.net.com/ExecuteEBSCommand";
public final static String OPERATION_NAME = "ExecuteEBSCommand";
public final static String NAMESPACE = "http://www.alpha.net.com";
public final static String URL = "http://192.168.2.100/Ebs2Alpha/Service.asmx";
public String connection(String Command, String CommandParameters) throws Throwable, Throwable {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("strCommand", Command);
Request.addProperty("strCommandParameters", CommandParameters);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
到目前為止,我通過調(diào)用主要活動(dòng)中的連接方法獲得響應(yīng)
So far I am getting the response by calling the connection method in main activity with
SoapCall call1= new SoapCall();
call1.connection("get_clients", "%");
推薦答案
使用 AsyncTask
很簡單.這是一個(gè)例子.
Using AsyncTask
is straightforward. Here is an example.
public class MyTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
String response = null;
SoapObject Request = new SoapObject(NAMESPACE, OPERATION_NAME);
Request.addProperty("strCommand", params[0]);
Request.addProperty("strCommandParameters", params[1]);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
// Needed to make the internet call
// Allow for debugging - needed to output the request
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION, soapEnvelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject) soapEnvelope.bodyIn;
response = result.getProperty(0).toString();
return response;
}
}
以及對帶參數(shù)的任務(wù)的調(diào)用.
And the call to the task with parameters.
MyTask myTask = new MyTask();
myTask.execute(new String[] {Command, CommandParameters});
希望它會有所幫助.
這篇關(guān)于如何在異步任務(wù)中進(jìn)行 ksoap2 調(diào)用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!