当前位置: 编程技术>移动开发
android调用web service(cxf)实例应用详解
来源: 互联网 发布时间:2014-10-13
本文导语: Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包 1.下载该夹包可以直接登录http://code.google.com/p/ksoap2-android/,现在该站点已经提供了直接的下载,只要点击下载链接就可以下载了; 我现在的是ksoap2-android-assem...
Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包
1.下载该夹包可以直接登录http://code.google.com/p/ksoap2-android/,现在该站点已经提供了直接的下载,只要点击下载链接就可以下载了;
我现在的是ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar
2.好了,现在我们就可以进行新建项目来进行测试了,首先我们先建立java服务端,这里的一些前期准备我就不说了(比如与spring的整合等示例),
由于这里重点是android客户端,java服务器端就直接给代码了
Interface:(这里提供了两个方法,一个传递的是简单字符串,另一个传递的是符合对象+集合)
package xidian.sl.service.webService;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import xidian.sl.service.impl.webService.StudentList;
@WebService
@SOAPBinding(style = Style.RPC)
public interface TestService {
public String getUser(@WebParam(name = "name")String name);
public StudentList getStuList();
}
implement:
package xidian.sl.service.impl.webService;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import xidian.sl.entity.Students;
import xidian.sl.service.webService.TestService;
@WebService(endpointInterface = "xidian.sl.service.webService.TestService")
public class TestServiceImpl implements TestService {
@Override
public String getUser(String name) {
System.out.println("客户端传递的名字为 = "+name);
return name;
}
@Override
public StudentList getStuList() {
System.out.println("该方法被调用");
List stuList = new ArrayList();
//第一个学生
Students stu1 = new Students();
stu1.setStuName("沈浪");
stu1.setStuNum("1006010054");
stu1.setStuSex("男");
stuList.add(stu1);
//第二个学生
Students stu2 = new Students();
stu2.setStuName("香香");
stu2.setStuNum("1006010043");
stu2.setStuSex("女");
stuList.add(stu2);
//将List集合封装成一个对象才能在webService中进行传递
StudentList studentList = new StudentList();
studentList.setStuList(stuList);
return studentList;
}
}
list的封装对象
package xidian.sl.service.impl.webService;
import java.util.List;
import xidian.sl.entity.Students;
public class StudentList {
private List stuList;
public List getStuList() {
return stuList;
}
public void setStuList(List stuList) {
this.stuList = stuList;
}
}
然后在srping的整合配置文件中进行如下配置即可(默认web.xml中已经进行配置)
3.到此服务器端的已经建立完全,我们可以测试下:开启tomcat,然后在浏览器中输入http://localhost:8090/WebExam/services/test?wsdl可以查看wsdl
现在我们就可以开始建立android客户端了
新建一个项目后导入ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar,这里要特别注意:导入包的方式不要选择项目右键---->build path---->
add external archives...,如果使用这种方式表面上好像是导入了包,但还是没有办法引用到,然后启动项目后一直会报:
我们还是选择和开发web一样的方式,就是在项目下新建lib或者libs文件夹,然后将jar直接复制到该文件夹中,IDE会帮助直接引入的:
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很简单就是两个编辑框:
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的注册和并添加访问网络的权限
运行后的结果如图所示:
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
开发android应用程序时,用java调用so库输出日志问题
Android调用系统时间格式显示时间信息
android 触屏的震动响应接口调用方法
android 能调用vc写好的静态库lib或者动态库DLL么?
Android调用默认浏览器打开指定Url的方法实例
Android调用google地图生成路线图实现代码
Android应用内调用第三方应用的方法
android 调用系统的照相机和图库实例详解
通过Html网页调用本地安卓(android)app程序代码
Android调用系统的发邮件功能的小例子
android动态壁纸调用的简单实例
编程语言
iis7站长之家
Android开发中怎样调用系统Email发送邮件(多种调用方式)
android 微信 sdk api调用不成功解决方案
Android调用相机并将照片存储到sd卡上实现方法
android中可以通过两种方式调用接口发送短信
android monkey自动化测试改为java调用monkeyrunner Api
android中soap协议使用(ksoap调用webservice)
android开发之调用手机的摄像头使用MediaRecorder录像并播放
1.下载该夹包可以直接登录http://code.google.com/p/ksoap2-android/,现在该站点已经提供了直接的下载,只要点击下载链接就可以下载了;
我现在的是ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar
2.好了,现在我们就可以进行新建项目来进行测试了,首先我们先建立java服务端,这里的一些前期准备我就不说了(比如与spring的整合等示例),
由于这里重点是android客户端,java服务器端就直接给代码了
Interface:(这里提供了两个方法,一个传递的是简单字符串,另一个传递的是符合对象+集合)
代码如下:
package xidian.sl.service.webService;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import xidian.sl.service.impl.webService.StudentList;
@WebService
@SOAPBinding(style = Style.RPC)
public interface TestService {
public String getUser(@WebParam(name = "name")String name);
public StudentList getStuList();
}
implement:
代码如下:
package xidian.sl.service.impl.webService;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import xidian.sl.entity.Students;
import xidian.sl.service.webService.TestService;
@WebService(endpointInterface = "xidian.sl.service.webService.TestService")
public class TestServiceImpl implements TestService {
@Override
public String getUser(String name) {
System.out.println("客户端传递的名字为 = "+name);
return name;
}
@Override
public StudentList getStuList() {
System.out.println("该方法被调用");
List stuList = new ArrayList();
//第一个学生
Students stu1 = new Students();
stu1.setStuName("沈浪");
stu1.setStuNum("1006010054");
stu1.setStuSex("男");
stuList.add(stu1);
//第二个学生
Students stu2 = new Students();
stu2.setStuName("香香");
stu2.setStuNum("1006010043");
stu2.setStuSex("女");
stuList.add(stu2);
//将List集合封装成一个对象才能在webService中进行传递
StudentList studentList = new StudentList();
studentList.setStuList(stuList);
return studentList;
}
}
list的封装对象
代码如下:
package xidian.sl.service.impl.webService;
import java.util.List;
import xidian.sl.entity.Students;
public class StudentList {
private List stuList;
public List getStuList() {
return stuList;
}
public void setStuList(List stuList) {
this.stuList = stuList;
}
}
然后在srping的整合配置文件中进行如下配置即可(默认web.xml中已经进行配置)
代码如下:
3.到此服务器端的已经建立完全,我们可以测试下:开启tomcat,然后在浏览器中输入http://localhost:8090/WebExam/services/test?wsdl可以查看wsdl
现在我们就可以开始建立android客户端了
新建一个项目后导入ksoap2-android-assembly-2.6.5-jar-with-dependencies.jar,这里要特别注意:导入包的方式不要选择项目右键---->build path---->
add external archives...,如果使用这种方式表面上好像是导入了包,但还是没有办法引用到,然后启动项目后一直会报:
我们还是选择和开发web一样的方式,就是在项目下新建lib或者libs文件夹,然后将jar直接复制到该文件夹中,IDE会帮助直接引入的:
这样就正确无误了,不再会报类无法引用到了
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很简单就是两个编辑框:
代码如下:
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的注册和并添加访问网络的权限
代码如下:
运行后的结果如图所示:
下面我们来试着调用回传符合对象的方法:
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