`
wenzongliang
  • 浏览: 460535 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Koap传递对象

阅读更多
要传输自定类型必须继承KvmSerializable接口

    public class User implements KvmSerializable {

        private String name = null;

        private int age = 0;

        

        @Override

        public Object getProperty(int arg0) {

         // TODO Auto-generated method stub

         Object res = null;

         switch(arg0){

          case 0:

           res = this.name;

           break;

          case 1:

           res = this.age;

           break;

          default:

           break;

         }

         return res;

        }

        @Override

        public int getPropertyCount() {

         // TODO Auto-generated method stub

         return 2;

        }

        @Override

        public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {

         // TODO Auto-generated method stub

         switch(arg0){

         case 0:

          arg2.type = PropertyInfo.STRING_CLASS;

          arg2.name = "Name";

          break;

         case 1:

          arg2.type = PropertyInfo.INTEGER_CLASS;

          arg2.name = "Age";

          break;

         default:

          break;

        }

        }

        @Override

        public void setProperty(int arg0, Object arg1) {

         // TODO Auto-generated method stub

         if(arg1 == null) return;

         switch(arg0){

          case 0:

           this.name = arg1.toString();

           break;

          case 1:

           this.age = Integer.valueOf(arg1.toString());

           break;

          default:

           break;

         }

        }

       }

 


    //调用webservice的具体方法

 

    public String SayHello(){

      String nameSpace = "http://tempuri.org/";

      String methodName = "HelloWorld";

      String soapAction = "http://tempuri.org/HelloWorld";

 

      String url = "http://192.168.2.51/Default.asmx?wsdl";//后面加不加那个?wsdl参数影响都不大

      

      //建立webservice连接对象

      org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url);

      transport.debug = true;//是否是调试模式

      

      //设置连接参数

      //重要必须的

      SoapObject soapObject = new SoapObject(nameSpace, methodName);

      User user = new User();

      user.setProperty(0, "zhi");

      user.setProperty(1, 18);

      PropertyInfo pi = new PropertyInfo();

      pi.setName("user");//webservice接口的参数名,大小写必须跟dotnet中的webservice暴露出来的名字一致

      pi.setValue(user);

      pi.setType(user.getClass());

      soapObject.addProperty(pi);//将自定参数加入请求对象中

      

      //设置返回参数

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//soap协议版本必须用SoapEnvelope.VER11(Soap V1.1)

      envelope.dotNet = false;//注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 不指定rpc方式则用true否则要用false

      //envelope.bodyOut = transport;

      envelope.setOutputSoapObject(soapObject);//设置请求参数

      //重要必须的

      envelope.addMapping(nameSpace, "User", user.getClass());//传对象时必须,参数namespace是webservice中指定的, name是服务器类型的名称, claszz是自定义类的类型

 

      

      try {

           transport.call(soapAction, envelope);

           SoapObject sb = (SoapObject)envelope.bodyIn;//服务器返回的对象存在envelope的bodyIn中

            //重要必须的

           User us= (User)envelope.getResponse();//直接将返回值强制转换为已知对象

           return us.getName() + us.getAge();

      } catch (IOException e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

      } catch (XmlPullParserException e) {

       // TODO Auto-generated catch block

       e.printStackTrace();

      } catch(Exception ex){

       ex.printStackTrace();

      }

      return "";

 }

我的话费充值店-各种面额

电信100元仅售98.60 
联通100仅售99.00
移动100仅售99.30

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics