当我们需要在Android应用中调用网络服务时,可以通过使用Web Service来实现。在Android中使用ksoap2库可以简单地实现Web Service调用。下面是使用ksoap2库在Android中调用WebService的步骤示例。
步骤一:导入ksoap2库文件
将ksoap2的jar文件拷贝到Android项目的libs目录下,并在build.gradle中添加以下代码
dependencies {
implementation files('libs/ksoap2-android-assembly-3.6.2-jar-with-dependencies.jar')
}
步骤二:创建android异步任务
由于网络请求是不允许在主线程中进行的,所以我们需要创建Android异步任务来处理。
private class MyAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String result = "";
String url = "http://webServiceUrl";//Web Service请求URL
String namespace = "http://webServiceNamespace";//Web Service的命名空间
String methodName = "webServiceMethodName";//Web Service的方法名
try{
SoapObject soapObject = new SoapObject(namespace, methodName);//创建SoapObject对象
soapObject.addProperty("arg1", "paramValue1");//添加参数
soapObject.addProperty("arg2", "paramValue2");
SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//创建SoapSerializationEnvelope对象
soapSerializationEnvelope.bodyOut = soapObject;//设置参数
soapSerializationEnvelope.dotNet = true;
HttpTransportSE httpTransportSE = new HttpTransportSE(url);//创建HttpTransportSE对象
httpTransportSE.debug = true;
try{
httpTransportSE.call(null, soapSerializationEnvelope);//调用Web Service方法
if (soapSerializationEnvelope.getResponse() != null){
result = soapSerializationEnvelope.getResponse().toString();//获取返回结果
}
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//处理请求结果
}
}
步骤三:执行异步任务
执行异步任务的方式很简单,只需要调用MyAsyncTask的execute方法即可:
new MyAsyncTask().execute();
示例1:调用返回结构体的Web Service方法
要调用返回结构体的Web Service方法,需要创建一个SoapObject来存储传递的参数。
private void callWebService(){
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String url = "http://www.webservicex.net/globalweather.asmx?WSDL";
String namespace="http://www.webserviceX.NET";
String methodName="GetCitiesByCountry";
String soapAction="http://www.webserviceX.NET/GetCitiesByCountry";
String result = "";
try{
SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("CountryName", "China");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
HttpTransportSE httpTransportSE = new HttpTransportSE(url);
httpTransportSE.call(soapAction, envelope);
Object object = envelope.getResponse();
if (object != null) {
result = object.toString();
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, "Result:"+result);
//处理请求结果
}
};
task.execute();
}
示例2:调用返回数组结构体的Web Service方法
要调用返回数组结构体的Web Service方法,需要创建一个SoapObject来存储传递的参数。
private void callWebService2(){
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
String url = "http://www.wsdemo.com/WeatherWebService.asmx?WSDL";
String namespace="http://www.WSDemo.com";
String methodName="getWeatherInfoByCity";
String soapAction="http://www.WSDemo.com/getWeatherInfoByCity";
String result = "";
try{
SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("theCityName", "上海");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
HttpTransportSE httpTransportSE = new HttpTransportSE(url);
httpTransportSE.call(soapAction, envelope);
Vector vector = (Vector)envelope.getResponse();
if (vector != null && vector.size()>0) {
for(int i = 0; i < vector.size(); i++){
Object object = vector.get(i);
if (object instanceof SoapObject){
SoapObject soapObject1 = (SoapObject)object;
//处理返回的数据
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, "Result:"+result);
//处理请求结果
}
};
task.execute();
}
以上就是在Android中利用ksoap2调用WebService的示例代码详细攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android 中利用 ksoap2 调用 WebService的示例代码 - Python技术站