现在假设调用webservice方法的时候需要传一个对象参数,需要用到服务器端的一个类。

服务器端类是这样的

public class User implements Serializable {    private String username;    private Integer age;    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }}


webservice接口如下

/** * SERVICE_URL:http://192.168.1.101:8080/TestWsImplService/TestWsImpl * SERVICE_NAMESCAPE:http://impl.webservices.yinke.com/ * 测试服务,包含两个方法,一个返回单个字符串,一个返回字符串数组 * @author Tsingheng */@Local@WebServicepublic interface TestWs {    /**     * 返回单个字符串测试方法     * @param testStr 字符串参数,将包含在返回结果中     * @return 返回单个字符串     */    String test(User user);    /**     * 返回字符串数组测试方法     * @return 返回字符串数组     */    String[] arrayTest();}


然后看客户端对应的User类,需要实现KvmSerializable接口

public class User implements KvmSerializable {private String username;private Integer age;public User() {}public User(SoapObject obj) {this.username = obj.getProperty(0).toString();this.username = obj.getProperty(1).toString();}@Overridepublic Object getProperty(int arg0) {// TODO Auto-generated method stubObject obj = null;switch (arg0) {case 0: {obj = this.username;break;}case 1: {obj = this.age;break;}}return obj;}@Overridepublic int getPropertyCount() {// TODO Auto-generated method stubreturn 2;}@SuppressWarnings("rawtypes")@Overridepublic void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {// TODO Auto-generated method stubswitch(arg0){case 0:{arg2.name = "username";arg2.type = PropertyInfo.STRING_CLASS;break;}case 1:{arg2.name = "age";arg2.type = PropertyInfo.STRING_CLASS;break;}}}@Overridepublic void setProperty(int arg0, Object arg1) {// TODO Auto-generated method stubswitch(arg0){case 0:{this.username = arg1.toString();break;}case 1:{this.age = (Integer) arg1;break;}}}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:id="@+id/linearLayout"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <TextView         android:id="@+id/testStr"        android:text="@string/hello_world"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>


public class MainActivity extends Activity {private TextView testStr;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);testStr = (TextView) super.findViewById(R.id.testStr);try {User user = new User();user.setUsername("Tsingheng");user.setAge(22);String resultText = stringTest(user);testStr.setText(resultText);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XmlPullParserException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//测试调用getResultArray方法,返回值为字符串数组。private String stringTest(User user) throws IOException, XmlPullParserException{SoapObject arraySoapObject = SoapUtil.buildSoapObject(ServiceUtil.TEST_METHOD);//为方法调用添加参数PropertyInfo pi = new PropertyInfo();pi.setName("arg0");pi.setValue(user);pi.setType(User.class);arraySoapObject.addProperty(pi);String result = SoapUtil.getResultString(ServiceUtil.TEST_SERVICE_URL, arraySoapObject);return result;}}


运行结果如下

需要用到的Util类

public class ServiceUtil {public static final String SERVER_IP = "192.168.1.101:8080";public static final String SERVICE_NS = "http://impl.webservice.yinke.com/";public static final String REGIST_SERVICE_URL = "http://" + SERVER_IP + "/RegistWsImplService/RegistWsImpl";public static final String OPERATION_SERVICE_URL = "http://" + SERVER_IP + "/OperationWsImplService/OperationWsImpl";public static final String MESSAGE_SERVICE_URL = "http://" + SERVER_IP + "/MessageWsImplService/MessageWsImpl";public static final String TEST_SERVICE_URL = "http://" + SERVER_IP + "/TestWsImplService/TestWsImpl";public static final String REGIST_METHOD = "register";public static final String UP_OPERATION_METHOD = "uploadOperation";public static final String CHECK_MESSAGE_METHOD = "hasNewMessage";public static final String GET_MESSAGEID_METHOD = "getStationMessageIds";public static final String GET_MESSAGE_METHOD = "getMessageById";public static final String MESSAGE_CONFIRM_METHOD = "messageComfirm";public static final String TEST_METHOD = "test";public static final String ARRAY_TEST_METHOD = "arrayTest";}


public class SoapUtil {public static SoapObject buildSoapObject(String methodName) {SoapObject soapObject = new SoapObject(ServiceUtil.SERVICE_NS,methodName);return soapObject;}public static SoapObject resultSoapObject(String serviceUrl,SoapObject soapObject) throws IOException, XmlPullParserException {HttpTransportSE ht = new HttpTransportSE(serviceUrl);ht.debug = true;SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = soapObject;ht.call(null, envelope);if (envelope.getResponse() != null) {SoapObject resultSoapObject = (SoapObject) envelope.bodyIn;return resultSoapObject;}return null;}public static String getResultString(String serviceUrl,SoapObject soapObject) throws IOException, XmlPullParserException{SoapObject resultSoapObject = resultSoapObject(serviceUrl, soapObject);if (resultSoapObject != null) return String.valueOf(resultSoapObject.getProperty(0));return null;}public static String[] getResultArray(String serviceUrl,SoapObject soapObject) throws IOException, XmlPullParserException{SoapObject resultSoapObject = resultSoapObject(serviceUrl, soapObject);if (resultSoapObject != null){String[] result = new String[resultSoapObject.getPropertyCount()];for(int i = 0; i < resultSoapObject.getPropertyCount(); i++){result[i] = String.valueOf(resultSoapObject.getProperty(i));}return result;}return null;}}


个人觉得如果能不用服务器端类就不要用。OVER,谢谢您的阅读!

更多相关文章

  1. 分支和循环(二)(零基础学习C语言)
  2. Android(安卓)Fragment详解(二):Fragment创建及其生命周期
  3. Android(安卓)中使用自定义字体的方法
  4. 每次吃一点Androidの小知识
  5. Android(安卓)实现事件监听器的五种处理方法
  6. android 初识EventBus
  7. Android(安卓)解决getColor()或getDrawable()方法过时的办法
  8. Android获取手机通话记录的方法
  9. 【安卓笔记】Volley全方位解析,带你从源码的角度彻底理解

随机推荐

  1. android UI事件
  2. 在Android(安卓)Sudio中使用Uiautomator
  3. 源码编译Android(安卓)4.1.2的Camera应用
  4. Android的用户界面
  5. android 自定义通知消息设置背景色不生效
  6. [Traceview]android性能测试
  7. Android(安卓)LCD(四):LCD驱动调试篇
  8. Android中文翻译组
  9. Ubuntu下Android(安卓)NDK的安装及配置
  10. Activity之间传递类对象