[英]how to use asynctask properly with ksoap2 and axis2 web service
我使用Web服務向導從動態Web項目開發了Web服務(遵循了教程,因此我不確定自己在做什么)。 本教程使我安裝了axis2插件,因此我假設這是用於Web服務生成的插件。 無論如何,當我從瀏覽器中嘗試該服務時,該服務正在運行,因此該部分沒有問題。
我的問題是,我想從android應用程序訪問此Web服務。 我在過去兩天里讀了很多教程,並嘗試實現其中一個,但是后來意識到它已經過時了,它試圖在主線程中連接到網絡,而在android 3.0之后是不允許的。 然后我發現了有關asynctask的問題,並嘗試了一下,但是有很多我不理解的地方。 我發現的教程沒有任何解釋,所以我在這里問。
我在聽說asynctask之前編寫的代碼在這里:
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String NAMESPACE = "http://eko.erdem.com";
private final String URL = "http://159.20.89.38:9398/EkoBiletTest/services/EkoClass?wsdl";
private final String SOAP_ACTION = "http://eko.erdem.com/homePage";
private final String METHOD_NAME = "homePage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String place = "Paris";
String checkinDate = "2013-06-10";
String checkoutDate = "2013-06-12";
//Pass value for place variable of the web service
PropertyInfo placeProp =new PropertyInfo();
placeProp.setName("place");//Define the variable name in the web service method
placeProp.setValue(place);//Define value for place variable
placeProp.setType(String.class);//Define the type of the variable
request.addProperty(placeProp);//Pass properties to the variable
//Pass value for checkinDate variable of the web service
PropertyInfo checkinDateProp =new PropertyInfo();
checkinDateProp.setName("checkinDate");
checkinDateProp.setValue(checkinDate);
checkinDateProp.setType(String.class);
request.addProperty(checkinDateProp);
//Pass value for checkinDate variable of the web service
PropertyInfo checkoutDateProp =new PropertyInfo();
checkoutDateProp.setName("checkoutDate");
checkoutDateProp.setValue(checkoutDate);
checkoutDateProp.setType(String.class);
request.addProperty(checkoutDateProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(response.toString());
setContentView(tv);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
現在我不知道代碼的哪一部分應該在asynctask的哪個函數中(doinBackground()等)。我也不了解asynctask的參數發送系統。 例如,如果我想將NAMESPACE,URL,METHOD_NAME等傳遞給asynctask,該怎么辦? 另外,我應該在哪里更新主要活動的textView? 如果有人可以指出涉及這些問題的教程,那就太好了。 對於您對這些問題的任何回答,我也將不勝感激。 提前致謝。
任何與網絡相關的工作都需要在AsyncTask
的doInBackground
方法中完成
要將數據發送到asynctask,您需要聲明該類的傳入數據
puclic class MyNetowrkTask extends AsyncTask<String[],Void,Void>
<>
的第一個參數是即將到來的數據,第二個參數是進度,第三個是完成任務后返回到onPostExecute
的結果。
所以要傳遞字符串,你會做這樣的事情
new MyNetworkTask().execute(new String[] {"namespace","url","method"});
在doInBackground
您將獲得像這樣的數據
protected Void doInBackground(String... params) {
String[] data = params[0];
String namespace = data[0];
String url = data[1];
String method = data[2];
....
}
UI元素的任何更新都需要在onPostExecute中完成,因此,如果要更新textview,則需要在此處進行。
代碼並不完全正確,但這會讓您開始了解
現在我不知道代碼的哪個部分應該在asynctask的哪個函數中(doinBackground()等)。
基本上,將大部分工作放在doInBackground()
,並確保不要嘗試從那里更新UI
。 其他任何方法都可以更新UI
。 這是顯示基本結構的答案 。
我也不了解asynctask的參數發送系統。 例如,如果我想將NAMESPACE,URL,METHOD_NAME等傳遞給asynctask,該怎么辦?
如果要在doInBackground()
使用這些參數,可以將它們作為vargs在execute()
函數中傳遞,否則可以在構造函數中傳遞它們
MyTask task = new MyTask(); // pass variables here to go to the constructor`
task.execute() // anything passed here will be received by doInBackground()`
另外,我應該在哪里更新主要活動的textView?
可以使用doInBackground()
之外的任何方法來完成此操作,具體取決於您何時需要更新它(任務完成之前,之中或之后)
如果這是一個內部類的的Activity
,那么你有一個成員變量Activity
提供給任務沿着Context
。 如果它是一個單獨的文件,則可能需要將Context
傳遞給任務的constructor
。
您應該將執行HTTP請求的代碼(可能在try-catch塊中的任何內容)放入異步任務的doInBackground()方法中。 如果要傳遞各種參數,可以將它們封裝到一個對象中並傳遞該對象。 或者,您可以將它們通過構造函數傳遞給異步任務,並在doInBackground()中使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.