문제

사람이 좋은 복잡한 물체 정렬 예제를 사용하 kSOAP 패키지?

도움이 되었습니까?

해결책

이 예제되지 않은 컴파일할 수 있고,완전한 기본적인 아이디어가 있는 클래스를 알려줍 kSOAP 를 설정하는 방법 XML 태그를 객체로(즉readInstance())과 설정하는 방법으로 물체 XML 태그(i.ewriteInstance()).

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);
  }
}

나중에 호출하는 경우 비누 서비스,당신은 지도체형(이 경우,파일을 개체)를 정렬하는 클래스입니다.비누 봉투 자동으로 일치시키는 개체의 유형의 각각 인수하지 않은 경우,내장형 호출하여 관련된 마샬러로 변환하기 위해서는 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 인코딩하수 파일의 개체입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top