【问题标题】:kSOAP Marshalling help needed需要 kSOAP 编组帮助
【发布时间】:2008-09-16 21:09:09
【问题描述】:

有没有人有一个使用 kSOAP 包的复杂对象编组的好例子?

【问题讨论】:

    标签: soap java-me ksoap


    【解决方案1】:

    虽然这个例子不是可编译和完整的,但基本思想是有一个类告诉 kSOAP 如何将 XML 标记转换为对象(即 readInstance())以及如何将对象转换为 XML 标记(即writeInstance())。

    public class MarshalBase64File implements Marshal {
    
      public static Class FILE_CLASS = File.class;
    
      public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected)
          throws IOException, XmlPullParserException {
        return Base64.decode(parser.nextText());
      }
    
      public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
        File file = (File)obj;
        int total = (int)file.length();
        FileInputStream in = new FileInputStream(file);
        byte b[] = new byte[4096];
        int pos = 0;
        int num = b.length;
        if ((pos + num) > total) {
          num = total - pos;
        }
        int len = in.read(b, 0, num);
        while ((len != -1) && ((pos + len) < total)) {
          writer.text(Base64.encode(b, 0, len, null).toString());
          pos += len;
          if ((pos + num) > total) {
            num = total - pos;
          }
          len = in.read(b, 0, num);
        }
        if (len != -1) {
          writer.text(Base64.encode(b, 0, len, null).toString());
        }
      }
    
      public void register(SoapSerializationEnvelope cm) {
        cm.addMapping(cm.xsd, "base64Binary", MarshalBase64File.FILE_CLASS, this);
      }
    }
    

    稍后,当您调用 SOAP 服务时,您会将对象类型(在本例中为 File 对象)映射到编组类。 SOAP 信封将自动匹配每个参数的对象类型,如果它不是内置类型,则调用关联的编组器将其转换为 XML。

    public class MarshalDemo {
    
      public String storeFile(File file) throws IOException, XmlPullParserException {
        SoapObject soapObj = new SoapObject("http://www.example.com/ws/service/file/1.0", "storeFile");
        soapObj.addProperty("file", file);
    
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        new MarshalBase64File().register(envelope);
        envelope.encodingStyle = SoapEnvelope.ENC;
        envelope.setOutputSoapObject(soapObj);
    
        HttpTransport ht = new HttpTransport(new URL(server, "/soap/file"));
        ht.call("http://www.example.com/ws/service/file/1.0/storeFile", envelope);
    
        String retVal = "";
        SoapObject writeResponse = (SoapObject)envelope.bodyIn;
        Object obj = writeResponse.getProperty("statusString");
        if (obj instanceof SoapPrimitive) {
          SoapPrimitive statusString = (SoapPrimitive)obj;
          String content = statusString.toString();
          retVal = content;
        }
        return retVal;
      }
    }
    

    在这种情况下,我使用 Base64 编码来编组 File 对象。

    【讨论】:

    • 不错的答案 +1 @Dan Howard
    • 如何为包含arraylist的自定义类编写marshal?
    猜你喜欢
    • 2021-01-16
    • 2014-04-06
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    相关资源
    最近更新 更多