以前一直都用HttpURLConnection 或 HttpClient 向服务器发布和接收数据。
这次因为项目需求使用 webService 来与服务端交互。
这里主要介绍webService来传递负责对象,传递简单数据可参考其它文章。
需要下载一个 ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar 的包.
final static String SERVICE_NS = "http://service.web.rt.org/";
final static String SERVICE_URL = "http://192.168.2.116:8080/webservice/UserService";
//调用的方法
String methodName = "save";此方法为你要调用服务器端的方法名称
//创建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);
这里我们要传递一个User对象给服务器端 封装此User对象
User user1 = new User();
user1.setPassword("123");
user1.setUserID("A662");
user1.setUsername("charles");
这里的setName 一定要与服务器接收端的参数名一致,否则服务器端无法获得
PropertyInfo property = new PropertyInfo();
property.setName("arg0");
property.setValue(user1);
property.setType(User.class);
request.addProperty(property);
envelope.addMapping(SERVICE_NS, "User", user1.getClass());
envelope.bodyOut = ht;
envelope.setOutputSoapObject(request);
//将SoapObject对象设置为SoapSerializationEnvelope对象的传出SOAP消息
try{
//调用webService
ht.call(null, envelope);
if(envelope.getResponse() != null){
SoapObject result = (SoapObject) envelope.bodyIn;
System.out.println("result>>>>>>"+result);
User user = new User();
SoapObject soapChildsChilds = (SoapObject)result.getProperty(0);
user = (User) JsonToBean.json2Bean(soapChildsChilds,User.class);//本人利用反射改写JsonToBean来获取user对象
System.out.println(user);
}catch (Exception e) {
e.printStackTrace();
}
下面是自己定义的User对象
public class User implements KvmSerializable {
private Stringpassword;
private StringuserID;
private Stringusername;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "username :"+username+"password :"+password+"userID "+userID;
}
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return password;
case 1:
return userID;
case 2:
return username;
}
return null;
}
public int getPropertyCount() {
// TODO Auto-generated method stub
return 3;
}
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch (arg0) {
case 0:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "password";//此处为解析xml标签时使用 注意引号一定不要少
break;
case 1:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "userID";
break;
case 2:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name = "username";
break;
}
}
public void setProperty(int arg0, Object arg1) {
switch (arg0) {
case 0:
password = arg1.toString();
break;
case 1:
userID = arg1.toString();;
break;
case 2:
username = arg1.toString();;
break;
}
}
}
个人感觉每次都需要实现KvmSerializable接口比较麻烦,目前还没找到比较好的方法来解决这个问题。