[英]How can I make a ksoap2 call in async task?
我是android開發的新手。 我正在嘗試開發一個將與 .net webservice 連接以檢索數據的應用程序。 我想使用AsyncTask
進行ksoap2調用。 我怎么把它asyncronus用的AsyncTask?
我的 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;
}
}
到目前為止,我通過在主要活動中調用連接方法來獲得響應
SoapCall call1= new SoapCall();
call1.connection("get_clients", "%");
使用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});
希望它會有所幫助。
我建議您使用 AsyncTaskLoader,根據我的口味,它比 AsyncTask 更容易。 看看這里,這個例子非常廣泛,一開始看起來很嚇人,你可能會發現更簡單的例子。 這個想法是你的 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) {
}
}
這就是你從任何你想要的地方(在那個活動中)啟動加載器的方式:
StartupCallback startupCallback = new StartupCallback();
getSupportLoaderManager().initLoader(0, null, startupCallback);
其中 0 是您提供給加載程序的 ID,null 是一組參數。 祝你好運 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.