欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android通过ksoap2传递复杂数据类型及CXF发布的webservice详细介绍

程序员文章站 2023-12-16 10:10:28
 android通过ksoap2传递复杂数据类型及cxf发布的webservice详细介绍 最近在学校搞点东西,搞了2天的webservice,心累呀,今天中午...

 android通过ksoap2传递复杂数据类型及cxf发布的webservice详细介绍

最近在学校搞点东西,搞了2天的webservice,心累呀,今天中午和小伙伴终于弄通了,感觉就是一些细节问题没有注意到,啊,我的时间呀,进这么过去了,为了不让小伙伴们走弯路,我还是认真的把开发文档写一遍吧!

首先,如果我们要用cxf发布webservice用自定义类型的对象来当参数传递的话,我们应该先把这个类序列化一遍,下面就是我测试的代码,我创建了一个tgrade类,实现了kvmserializable接口,这个接口里面的三个方法,这个接口的好处在于不需要服务端在去反序列化实体对象了,

public class tgrade implements kvmserializable { 
 
  // fields 
 
  private integer gid; 
  private integer gmax; 
  private integer gmin; 
  private string gname; 
  private string gpic; 
  private string gtype; 
   
  // constructors 
  /** default constructor */ 
  public tgrade() { 
  } 
 
  /** minimal constructor */ 
  public tgrade(integer gmax) { 
    this.gmax = gmax; 
  } 
 
  /** full constructor */ 
  public tgrade(integer gmax, integer gmin, string gname, string gpic, 
      string gtype) { 
    this.gmax = gmax; 
    this.gmin = gmin; 
    this.gname = gname; 
    this.gpic = gpic; 
    this.gtype = gtype; 
  } 
 
  // property accessors 
  public integer getgid() { 
    return this.gid; 
  } 
 
  public void setgid(integer gid) { 
    this.gid = gid; 
  } 
 
  public integer getgmax() { 
    return this.gmax; 
  } 
 
  public void setgmax(integer gmax) { 
    this.gmax = gmax; 
  } 
 
  public integer getgmin() { 
    return this.gmin; 
  } 
 
  public void setgmin(integer gmin) { 
    this.gmin = gmin; 
  } 
 
  public string getgname() { 
    return this.gname; 
  } 
 
  public void setgname(string gname) { 
    this.gname = gname; 
  } 
 
  public string getgpic() { 
    return this.gpic; 
  } 
 
  public void setgpic(string gpic) { 
    this.gpic = gpic; 
  } 
 
  public string getgtype() { 
    return this.gtype; 
  } 
 
  public void setgtype(string gtype) { 
    this.gtype = gtype; 
  } 
 
  @override 
  public object getproperty(int arg0) { 
    switch (arg0) {  
    case 0:  
      return gid;  
    case 1:  
      return gmax;  
    case 2:  
      return gmin;  
    case 3:  
      return gname;  
    case 4:  
      return gpic; 
    case 5:  
      return gtype;  
    default:  
      break;  
    }  
    return null;  
  } 
 
  @override 
  public int getpropertycount() { 
    // todo auto-generated method stub 
    return 6;//y要注意这里,必须等于参数的个数,不然服务端没有办法接受有些参数 
  } 
 
  @override 
  public void getpropertyinfo(int arg0, hashtable arg1, propertyinfo arg2) { 
    switch (arg0) { 
    case 0:  
      arg2.type = propertyinfo.string_class;  
      arg2.name = "gid";  
      break;  
    case 1:  
      arg2.type = propertyinfo.string_class;  
      arg2.name = "gmax";  
      break;  
    case 2:  
      arg2.type = propertyinfo.string_class;  
      arg2.name = "gmin";  
      break;  
    case 3:  
      arg2.type = propertyinfo.string_class;  
      arg2.name = "gname";  
      break;  
    case 4:  
      arg2.type = propertyinfo.string_class;  
      arg2.name = "gpic";  
      break;  
    case 5:  
      arg2.type = propertyinfo.string_class;  
      arg2.name = "gtype";  
      break;  
    default:  
      break;  
    }     
  } 
 
  @override 
  public void setproperty(int arg0, object arg1) { 
    switch (arg0) { 
    case 0: 
      gid=integer.parseint(arg1.tostring()); 
      break; 
    case 1: 
      gmax=integer.parseint(arg1.tostring()); 
 
      break; 
    case 2: 
      gmin=integer.parseint(arg1.tostring()); 
 
      break; 
    case 3: 
      gname=arg1.tostring(); 
 
      break; 
    case 4: 
      gpic=arg1.tostring(); 
 
      break; 
    case 5: 
 
      gtype=arg1.tostring(); 
      break; 
 
    default: 
      break; 
    } 
  } 
 
 
 
} 
 
//-----------------------------下面是我测试部分的代码,这部分代码很重要,需要认真的看,我也写的比较详细,代码的世界模糊不得 
 
public boolean addmaintenanceinfo() { 
    string methodname = "addgrade";//服务端的方法 
    string soapaction =“http://10.127.80.67/gbckf/android/gradeservice”+methodname; 
     
    tgrade person = new tgrade(); 
    person.setproperty(0, "6"); 
    person.setproperty(1, 1); 
    person.setproperty(3, "1"); 
    person.setproperty(4, "1"); 
    person.setproperty(5, "1"); 
    // 建立webservice连接对象 
    httptransportse transport = new httptransportse(agbcapi.gradeserviceurl,5000);//5秒超时 
    transport.debug = true;// 是否是调试模式 
    // 设置连接参数 
    soapobject soapobject = new soapobject(agbcapi.namespace, methodname); 
    propertyinfo objekt = new propertyinfo(); 
    objekt.setname("arg0");//这个arg0很重要,不能是其他的东西,只能是arg0,不要问我为何,不然你就永远接受不了参数,因为是xml文档类型的东西 
    objekt.setvalue(person); 
    objekt.settype(tgrade.class); 
    soapobject.addproperty(objekt); 
    // 设置返回参数 
    soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11);// soap协议版本必须用soapenvelope.ver11(soap 
    envelope.dotnet = false;// 注意:这个属性是对dotnetwebservice协议的支持,如果dotnet的webservice 
    envelope.bodyout = transport; 
    log.i("请求参数", soapobject.tostring()); 
    envelope.setoutputsoapobject(soapobject);// 设置请求参数 
      envelope.addmapping(agbcapi.namespace, "addgrade", tgrade.class);// 传对象时必须,参数namespace是webservice中指定的, 
      (new marshalbase64()).register(envelope); 
      try { 
      transport.call(soapaction, envelope); 
      if(envelope.bodyin instanceof soapfault){ 
        string str = ((soapfault) envelope.bodyin).faultstring; 
        log.i("空节点返回的东西", str); 
      }else { 
        // soapobject sb = (soapobject)envelope.bodyin;//服务器返回的对象存在envelope的bodyin中 
        object obj = envelope.getresponse();// 直接将返回值强制转换为已知对象 
        //log.d("webservice", "返回结果:" + obj.tostring()); 
      } 
    } 
    catch (ioexception e) { 
      e.printstacktrace(); 
    } 
    catch (xmlpullparserexception e) { 
      e.printstacktrace(); 
    } 
    catch (exception ex) { 
      ex.printstacktrace(); 
    } 
 
    return true; 

上面是我亲手写的代码,若是没有明白小伙伴,给我留言我给你看看吧,注意请求网络不能放在主线程哦,不然要报错的

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: