使用Ksoap Android制作复杂的请求

时间:2015-03-22 06:10:34

标签: android ksoap2 android-ksoap2 ksoap

我试图用嵌套的对象数组发出请求,wsdl看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
  <SOAP-ENV:Body>
    <ns1:PopulateCart>
      <ns1:sc>
        <ns1:CustomerID>52</ns1:CustomerID>
        <ns1:Email>testing@gmail.com</ns1:Email>
        <ns1:Contact>0161234444</ns1:Contact>
        <ns1:RestaurantID>251</ns1:RestaurantID>
        <ns1:TimePickup>2015-03-21T23:59:00</ns1:TimePickup>
        <ns1:VoucherCode></ns1:VoucherCode>
        <ns1:FoodItems>
          <ns1:FoodMainItem>
            <ns1:product_id>20754</ns1:product_id>
            <ns1:qty>1</ns1:qty>
          </ns1:FoodMainItem>
        </ns1:FoodItems>
        <ns1:FoodItems>
          <ns1:FoodMainItem>
            <ns1:product_id>20754</ns1:product_id>
            <ns1:qty>2</ns1:qty>
          </ns1:FoodMainItem>
        </ns1:FoodItems>
      </ns1:sc>
    </ns1:PopulateCart>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我正在使用Android的ksoap2库,我尝试过:

    // ApiConstant.java
    public static final String NAMESPACE = "http://tempuri.org/";
    public static final String KSOAP_BASE_URL = "http://dedev.delivereat.my/Onappservice.asmx";

    //PopulateCartTask.java
    String METHOD_NAME = "PopulateCart";
    String SOAP_ACTION = ApiConstant.NAMESPACE + METHOD_NAME;
    String URL = ApiConstant.KSOAP_BASE_URL;

    SoapObject request = new SoapObject(ApiConstant.NAMESPACE, METHOD_NAME);
    SoapObject sc = new SoapObject(ApiConstant.NAMESPACE,"sc");

    //CHANGE2
    PropertyInfo pi = new PropertyInfo();
    pi.setName("CustomerID");
    pi.setValue(52); //1
    pi.setType(String.class);
    sc.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("Email");
    pi.setValue("serene@greenroom.com.my"); //2
    pi.setType(String.class);
    sc.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("Contact");
    pi.setValue("0161235555"); //2
    pi.setType(String.class);
    sc.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("RestaurantID");
    pi.setValue("251"); //2
    pi.setType(String.class);
    sc.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("Email");
    pi.setValue("serene@greenroom.com.my"); //2
    pi.setType(String.class);
    sc.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("TimePickup");
    pi.setValue("2015-03-21T23:59:00"); //2
    pi.setType(String.class);
    sc.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("VoucherCode");
    pi.setValue(""); //2
    pi.setType(String.class);
    sc.addProperty(pi);

    SoapObject foodItems = new SoapObject(ApiConstant.NAMESPACE, "FoodItems");
    SoapObject FoodMainItem = new SoapObject(ApiConstant.NAMESPACE, "FoodMainItem");

    pi = new PropertyInfo();
    pi.setName("product_id");
    pi.setValue("20754"); //2
    pi.setType(String.class);
    FoodMainItem.addProperty(pi);

    pi = new PropertyInfo();
    pi.setName("qty");
    pi.setValue("1"); //2
    pi.setType(String.class);
    FoodMainItem.addProperty(pi);

    foodItems.addSoapObject(FoodMainItem);
    sc.addSoapObject(foodItems);
    request.addSoapObject(sc);

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

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapObject response = (SoapObject) envelope.getResponse();

但我收到以下错误:

03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ SoapFault - faultcode: 'soap:Client' faultstring: 'Server was unable to read request. ---> There is an error in XML document (1, 294). ---> The specified type was not recognized: name='sc', namespace='http://tempuri.org/', at <sc xmlns='http://tempuri.org/'>.' faultactor: 'null' detail: org.kxml2.kdom.Node@28028932
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:147)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:275)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at com.deliver.eat.async.PopulateCartTask.doInBackground(PopulateCartTask.java:203)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at com.deliver.eat.async.PopulateCartTask.doInBackground(PopulateCartTask.java:20)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-22 14:55:06.645    6282-6415/com.deliver.eat W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-22 14:55:06.646    6282-6415/com.deliver.eat W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-22 14:55:06.646    6282-6415/com.deliver.eat W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-22 14:55:06.646    6282-6415/com.deliver.eat W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-22 14:55:06.646    6282-6415/com.deliver.eat W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

我怀疑它与SoapObject命名空间有关,我查了很多链接,尝试了几种不同的方法,但是没有它们正在工作:

  • SoapObject sc = new SoapObject(ApiConstant.NAMESPACE,&#34; sc&#34;);
  • SoapObject sc = new SoapObject(ApiConstant.NAMESPACE,&#34; ns1:sc&#34;);
  • SoapObject sc = new SoapObject(null,&#34; sc&#34;);
  • SoapObject sc = new SoapObject(&#34;&#34;,&#34; sc&#34;);
  • SoapObject sc = new SoapObject(METHOD_NAME,&#34; sc&#34;);

问题:提出此请求的正确方法是什么?

我已经坚持了一段时间,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

我无法找到关于如何使用kso​​ap库的答案,所以我最终使用volley库进行POST请求以实现相同的效果,在获得响应之后,我使用xml解析器手动解析了响应。

所以这是我写的代码: https://gist.github.com/worker8/20eb5bd3400b88f10973

我是如何使用此代码的:

PriceConfirmationTask.Callback callback = new PriceConfirmationTask.Callback(){
    @Override
    public void onResponse(PriceCheckResult result) {

    Log.d("ddw", "result: " + result.toString());

    if (result != null) {
        showPriceConfirmationDialog(result);
    } else {
        Toast.makeText(mActivity, "Sorry, something went wrong... ", Toast.LENGTH_LONG).show();
    }}};

PriceConfirmationTask task = new PriceConfirmationTask(mActivity, "2015-04-01T13:00:00", callback);
task.callEndpoint();

可能有更好的方法,但如果你像我一样被困,这可能是一个很好的解决方案。

希望它有所帮助!