我是Android开发的新手。我正在尝试开发一个应用程序,它将与.net webservice连接以检索数据。我想用异步任务进行ksoap2调用。我如何用asynctask将其称为asyncronus?
我的SoapCall类是
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;
}
}
到目前为止,我通过使用
调用main活动中的连接方法来获得响应SoapCall call1= new SoapCall();
call1.connection("get_clients", "%");
答案 0 :(得分:2)
使用asynctask非常简单。这是一个例子。
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;
}
}
用参数调用任务。
MyTask myTask = new MyTask();
myTask.execute(new String[] {Command, CommandParameters});
希望它会有所帮助。
答案 1 :(得分:0)
我建议您使用AsyncTaskLoader,因为我的口味比AsyncTask更容易。
看看here,这个例子非常广泛,起初看起来很吓人,你可能会发现更简单的一些。我们的想法是,您的Activity实现了LoaderCallbacks
来创建加载器,以及在加载器完成时调用的方法。你通过LoaderManager
开始'加载'。
AsynctaskLoader是一个extends AsyncTaskLoader
的类,可以处理异步内容。
我会给你一个简单的例子:
这是AsyncTaskLoader:
public class StartupLoader extends AsyncTaskLoader<Boolean> {
Context context;
public StartupLoader(Context context) {
super(context);
this.context = context;
forceLoad();
}
@Override
public Boolean loadInBackground() {
// DO STUFF!
return true;
}
@Override
protected void onStopLoading() {
}
@Override
public void onCanceled(Boolean data) {
super.onCanceled(data);
}
@Override
protected void onReset() {
super.onReset();
}
}
这是启动加载器的Activity中的内容,它是一个内部类:
public class StartupCallback implements
LoaderManager.LoaderCallbacks<Boolean> {
@Override
public void onLoadFinished(Loader<Boolean> loader, Boolean succ) {
// Here you get your results back
}
@Override
public Loader<Boolean> onCreateLoader(int id, Bundle args) {
return new StartupLoader(getApplicationContext());
}
@Override
public void onLoaderReset(Loader<Boolean> loader) {
}
}
这就是你如何从你想要的(在那个Activity中)启动加载器:
StartupCallback startupCallback = new StartupCallback();
getSupportLoaderManager().initLoader(0, null, startupCallback);
其中0是您为加载程序提供的ID,null是一个参数包。 祝你好运:)