我可以使用KSoap2作为请求发送静态XML文件

时间:2015-07-15 18:17:52

标签: java android xml ksoap2

我正在使用KSoap2尝试在下面构建XML文件请求。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_5="www.444.com">

<soapenv:Header/>
  <soapenv:Body>
  <_5:GetStaff>
     <!--Optional:-->
     <_5:Request>
        <!--Optional:-->
        <_5:SourceCredentials>
           <!--Optional:-->
           <_5:SourceName>sourcename</_5:SourceName>
           <!--Optional:-->
           <_5:Password>password=</_5:Password>
           <!--Optional:-->
           <_5:SiteIDs>
              <!--Zero or more repetitions:-->
              <_5:int>1111</_5:int>
           </_5:SiteIDs>
              </_5:SourceCredentials>
          </_5:Request>
       </_5:GetStaff>
    </soapenv:Body>
</soapenv:Envelope>

我试图采用这个并生成以下代码,但似乎子元素导致了我的问题。我是Ksoap2的新手,无法找到明确的结构化解决方案。

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("SourceName", "sourcename");
        request.addProperty("Password", "password=");
        request.addProperty("int", "1111");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE htse = new HttpTransportSE(URL);

        try{
            htse.call(SOAP_ACTION, envelope);
            SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse();

           ret = resultString.toString();
        }
        catch(Exception e){
            e.printStackTrace();
        }

我如何使用KSoap2构建树,以便正确访问子元素。没有这种结构,api没有正确响应。

有没有办法直接从xml资源中发送这个xml文件?

还是有另一种方式吗?

更新:尝试为复杂元素制作类

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

import java.util.Hashtable;

/**
* Created by randypfohl on 7/15/15.
*/
public class SourceCredentials implements KvmSerializable {

//Xml variables
private String sourceName;
private String password;
private SiteIDs site;

public SourceCredentials() {
}

public void setSourceName(String sourceName) {
    this.sourceName = sourceName;
}

public String getSourceName() {
    return this.sourceName;
}

public void  setPassword(String password) {
    this.password = password;
}

public String getPassword() {
    return this.password;
}

public void setSiteIDs(SiteIDs site) {
    this.site = site;
}

@Override
public Object getProperty(int index) {

    Object toreturn = null;

    switch (index) {
        case 0:
            toreturn = this.sourceName;
        case 1:
            toreturn = this.password;
        case 2:
            toreturn = this.site;
        default:
            break;
    }
    return toreturn;
}

    @Override
    public int getPropertyCount () {
        return 3;
    }


        @Override
        public void setProperty ( int index, Object value){
            switch (index) {
                case 0:
                    this.sourceName = value.toString();
                    break;
                case 1:
                    this.password = value.toString();
                    break;
                case 2:
                    site.setProperty(0, value);
                    break;
            }

        }

        @Override
        public void getPropertyInfo ( int index, Hashtable hashtable, PropertyInfo propertyInfo){
        switch (index) {
            case 0:
                propertyInfo.name = "SourceName";
                propertyInfo.type = PropertyInfo.STRING_CLASS;
                break;
            case 1:
                propertyInfo.name = "Password";
                propertyInfo.type = PropertyInfo.STRING_CLASS;
                break;
            case 2:
                propertyInfo.name = "SiteIDs";
                propertyInfo.type = SiteIDs.class;
                break;
            default:
                break;
        }
    }

    @Override
    public String getInnerText () {
        return null;
    }

    @Override
    public void setInnerText (String s){

    }
}

这是siteids类。

import java.util.Hashtable;
import java.util.Vector;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class SiteIDs extends Vector<Integer> implements KvmSerializable {

@Override
public Object getProperty(int arg0) {
    return this.get(arg0);
}

@Override
public String getInnerText () {
    return null;
}

@Override
public void setInnerText (String s){

}

@Override
public int getPropertyCount() {
    return 1;
}

@Override
   public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
    arg2.name = "int";
    arg2.type = PropertyInfo.INTEGER_CLASS;
}

@Override
public void setProperty(int arg0, Object arg1) {
    this.add(Integer.valueOf(arg1.toString()));
}
}

这是我创建的代码。

/ * String ret =“fail”;

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SourceCredentials sourceCredentials = new SourceCredentials();
        sourceCredentials.setProperty(0, "name");
        sourceCredentials.setProperty(1, "password");

        SiteIDs siteIDS = new SiteIDs();
        siteIDS.setProperty(0, "1100");
        sourceCredentials.setSiteIDs(siteIDS);

        request.addProperty("SourceCredentials", sourceCredentials);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        envelope.addMapping(NAMESPACE, SourceCredentials.class.getSimpleName(), SourceCredentials.class);
        envelope.addMapping(NAMESPACE, SiteIDs.class.getSimpleName(), SiteIDs.class);

        try{
            HttpTransportSE htse = new HttpTransportSE(URL);
            htse.debug = true;
            htse.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->");
            htse.call(SOAP_ACTION, envelope);
            SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse();

           ret = resultString.toString();
            System.out.println(ret);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        return ret;

我无法理解。我似乎总是得到一个奇怪的错误。

 SoapFault - faultcode: 'soap:Server' faultstring: 'Server was unable to process request. ---> Object reference not set to an instance of an object.' faultactor: 'null' detail: org.kxml2.kdom.Node@b1eb5d78

0 个答案:

没有答案