今天图老师小编要向大家分享个android调用web service(cxf)实例应用详解教程,过程简单易学,相信聪明的你一定能轻松get!
【 tulaoshi.com - 编程语言 】
Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包
这样就正确无误了,不再会报类无法引用到了
android中通过webservice调用服务器端其实还是很简单的,只要按部就班的按照下面步骤进行即可:
(1)创建HttpTransportSE对象,该对象用于调用WebService操作
代码如下:
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
(2)创建SoapSerializationEnvelope对象
代码如下:
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope
(SoapEnvelope.VER11);
(3)创建SoapObject对象,创建该对象时需要传入所要调用的Web Service的命名空间和WebService方法名
代码如下:
SoapObject request = new SoapObject(SERVICE_NS, methodName);
(4)如果有参数传给Web Service服务器端,调用SoapObject对象的addProperty(String name, Object value)方法来设置参数,该方法的name参数指定参数名
注意:参数名不一定要与服务端的方法中的参数名相同,只要对应顺序相同即可;value参数指定参数值
代码如下:
request.addProperty("name", "1006010054");
(5)调用SoapSerializationEnvelope的setOutputSoapObject()方法,或者直接对bodyOut属性赋值,将前两步创建的SoapObject对象设为SoapSerializationEnvelope的传出SOAP消息体
代码如下:
envelope.bodyOut = request;
(6)调用对象的call()方法,并以SoapSerializationEnvelope作为参数调用远程的web service
代码如下:
ht.call(null, envelope);
(7)掉用完成后,访问SoapSerializationEnvelope对象的bodyIn属性,该属性返回一个SoapObject对象,该对象就代表Web service的返回消息,解析该对象,即可获得调用web service的返回值
代码如下:
SoapObject result = (SoapObject) envelope.bodyIn;
String name = result.getProperty(0).toString();
下面给书具体的实例:
mian.xml很简单就是两个编辑框:
代码如下:
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /
EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
requestFocus /
/EditText
EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" /
/LinearLayout
Activity:(该Activity调用了服务器端返回普通字符串的方法)
代码如下:
package xidian.sl.android.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class WebServiceSimpleDemo extends Activity{
final static String SERVICE_NS = "http://webService.service.sl.xidian/";
final static String SERVICE_URL = "http://192.168.1.103:8090/WebExam/services/test";
private EditText txt1;
private EditText txt2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
//调用的方法
String methodName = "getUser";
//创建httpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
//使用soap1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//实例化SoapObject对象
SoapObject request = new SoapObject(SERVICE_NS, methodName);
/**
* 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
* */
request.addProperty("name", "1006010054");
//将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
envelope.bodyOut = request;
try{
//调用webService
ht.call(null, envelope);
//txt1.setText("看看"+envelope.getResponse());
if(envelope.getResponse() != null){
txt2.setText("有返回");
SoapObject result = (SoapObject) envelope.bodyIn;
String name = result.getProperty(0).toString();
txt1.setText("返回值 = "+name);
}else{
txt2.setText("无返回");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
在AndroidManifest.xml进行Activity的注册和并添加访问网络的权限
代码如下:
?xml version="1.0" encoding="utf-8"?
manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.sl.android.webservice"
android:versionCode="1"
android:versionName="1.0"
uses-sdk android:minSdkVersion="10" /
application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
activity
android:name=".WebServiceSimpleDemo"
android:label="@string/app_name"
intent-filter
action android:name="android.intent.action.MAIN" /
category android:name="android.intent.category.LAUNCHER" /
/intent-filter
/activity
/application
!-- 声明该应用自身所拥有的权限 --
uses-permission android:name="android.permission.INTERNET" /
/manifest
运行后的结果如图所示:
下面我们来试着调用回传符合对象的方法:
activity:
代码如下:
package xidian.sl.android.webservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class WebServiceComplexDemo extends Activity{
final static String SERVICE_NS = "http://webService.service.sl.xidian/";
final static String SERVICE_URL = "http://192.168.1.103:8090/WebExam/services/test";
private EditText txt1;
private EditText txt2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
//调用的方法
String methodName = "getStuList";
//创建httpTransportSE传输对象
HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
ht.debug = true;
//使用soap1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//实例化SoapObject对象
SoapObject request = new SoapObject(SERVICE_NS, methodName);
/**
* 设置参数,参数名不一定需要跟调用的服务器端的参数名相同,只需要对应的顺序相同即可
* */
//request.addProperty("name", "1006010054");
//将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
envelope.bodyOut = request;
try{
//调用webService
ht.call(null, envelope);
txt2.setText("回传的值 :"+envelope.getResponse());
if(envelope.getResponse() != null){
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject soapChilds = (SoapObject)result.getProperty(0);
StringBuffer sb = new StringBuffer();
for(int i=0; i soapChilds.getPropertyCount(); i++){
SoapObject soapChildsChilds = (SoapObject)soapChilds.getProperty(i);
sb.append("姓名["+i+"] = "+soapChildsChilds.getProperty(0).toString()+"n");
sb.append("学号["+i+"] = "+soapChildsChilds.getProperty(1).toString()+"n");
sb.append("性别["+i+"] = "+soapChildsChilds.getProperty(2).toString()+"n"+"n");
}
txt1.setText(sb.toString());
}else{
txt1.setText("无返回");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
区别就是对于返回值的处理上,使用几次getPropert()方法,这里主要看返回值的层次,看下面的结果应该就能明白了,根据括号的层次来进行确定
来源:http://www.tulaoshi.com/n/20160219/1595967.html
看过《android调用web service(cxf)实例应用详解》的人还看了以下文章 更多>>