我使用 Web 服务向导从一个动态 Web 项目开发了一个 Web 服务(按照教程进行操作,所以我不确定自己在做什么)。该教程让我安装了axis2插件,所以我假设这是用于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?如果有人可以向我指出涵盖这些问题的教程,那就太好了。我也很感激您对这些问题的任何回答。提前致谢。