Nesting properties inside a tag in Ksoap2

荒凉一梦 提交于 2019-12-18 09:23:19

问题


Hi i want to make a request for soap using above soap xml

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:soap:functions:mc-style">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:AvailCheck>
         <IUvail>
            <Unit>PC</Unit>
            <Qty>3000</Qty>
         </IUvail>
      </urn:AvailCheck>
   </soapenv:Body>
</soapenv:Envelope>

So I created a code using ksoap library. i created an object for soapobject

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

and added the properties like this

request.addProperty("Unit", "PC");
request.addProperty("Qty", "3000");

but problem is i cant add <IUvail> in the requset.. So how can i add this?


回答1:


You can pass a complex object (IUvail) like this:

IUvail.java

import java.util.Hashtable;

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

public class IUvail implements KvmSerializable {
    private String unit;
    private int qty;

public IUvail() {}

public IUvail(String unit, int qty) {
    this.unit = unit;
    this.qty = qty;
}

public void setUnit(String unit) { this.unit = unit; }
public void setQty(int qty) { this.qty = qty;}
public String getUniy() { return unit;}
public int getQty() { return qty;}

public Object getProperty(int arg0) {
    switch(arg0) {
        case 0:
            return unit;
        case 1:
            return qty;
     }
     return null;
}

public int getPropertyCount() {
    return 2;
}

public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo propertyInfo) {
    switch(index){
        case 0:
            propertyInfo.name = "unit";
            propertyInfo.type = PropertyInfo.STRING_CLASS;
            break;
        case 1:
            propertyInfo.name = "qty";
            propertyInfo.type = PropertyInfo.INTEGER_CLASS;
            break;
        default:
            break;
    }
}

public void setProperty(int index, Object value) {
    switch(index) {
        case 0:
            this.unit = value.toString();
            break;
        case 1:
            this.qty = Integer.parseInt(value.toString());
            break;
        default:
            break;
   }
}  }

Then

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
IUvail iuvail = new IUvail();
iuvail.setUnit("PC");
iuvail.setQty(3000);
request.addProperty("IUvail", iuvail);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(TARGET_NAMESPACE, "IUvail", new IUvail.getClass());

AndroidHttpTransport transport = new AndroidHttpTransport(URL);
try {
    transport.call(NAMESPACE + METHOD_NAME, envelope);
    /* Get the response: it depends on your web service implementation */
} catch (Exception e) {
    e.printStackTrace();
}

Where NAMESPACE, URL, METHOD_NAME and TARGET_NAMESPACE depends on your ws.




回答2:


I had the same problem (accessing a SAP webservice with Android). There is an easy solution:

request.addProperty("IUvail", "<Unit>PC</Unit><Qty>3000</Qty>");

The whole access with authentication:

String USER = "user";
String PASSWORD = "pwd";
String NAMESPACE_SOAP = "http://schemas.xmlsoap.org/soap/envelope/";
String NAMESPACE_SAP = "urn:sap-com:document:sap:rfc:functions";
String SOAP_ACTION = "http://server:port/webservice_root";
String METHOD_NAME = "AvailCheck";

SoapObject request = new SoapObject(NAMESPACE_SAP, METHOD_NAME);
request.addProperty("IUvail", "<Unit>PC</Unit><Qty>3000</Qty>");

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
envelope.env = NAMESPACE_SOAP;
envelope.dotNet = false;
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(
        SOAP_ACTION);
androidHttpTransport.debug = true;
try {
    List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
    headerList.add(new HeaderProperty("Authorization", "Basic "
            + org.kobjects.base64.Base64
                    .encode((USER + ":" + PASSWORD).getBytes())));
    androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
    // SoapObject response = (SoapObject) envelope.getResponse();
    // response.getProperty(0).toString();
    // Object response = envelope.getResponse();
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    Log.d("webservice", "Result: " + result.toString());
} catch (Exception e) {
    e.printStackTrace();
    Log.e("WebserviceException", e.getMessage(), e);
    Log.d("WebserviceException", "[requestDump] "
            + androidHttpTransport.requestDump);
    Log.d("WebserviceException", "[responseDump] "
            + androidHttpTransport.responseDump);
}



回答3:


try doing something like this,

SoapObject request = new SoapObject(NAMESPACE,
                            METHOD_NAME);

                    request.addProperty("username", username);
                    request.addProperty("username", password);

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
                try {
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                    } catch (IOException ioex) {
                            ioex.printStackTrace();
                    }


来源:https://stackoverflow.com/questions/9805946/nesting-properties-inside-a-tag-in-ksoap2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!