如何在android中使用kso​​ap2来使用SAP SOAP Web服务

时间:2012-10-31 06:56:28

标签: soap sap soapui android-ksoap2

我想在我的Android应用中使用SAP webservices。 Web服务按预期在SOAPUI中工作(从sap wsdl生成的请求)。我尝试过使用kso​​ap2。当使用kso​​ap2时,我得到“Webservice处理错误”作为响应。我已经用尽了google / stackoverflow / sap社区网络的所有结果。我已尝试过在SOAPUI中从ksoap2发送的请求,但我无法确定它与工作(SOAPUI生成)请求之间的关键区别。请指导我。

ksoap2代码:

public class SoapRetail extends Activity {

private static final String NAMESPACE   = "urn:sap-com:document:sap:rfc:functions";
private static final String SERVICE_URL = "http://icanhazcheezburger.com/sap/bc/srt/rfc/sap/zretail_exec_topproducts_webs/100/zretail_exec_topproducts_webs/zretail_exec_topproducts_webs";
private static final String METHOD_NAME = "ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest";
private static final String SOAP_ACTION = "urn:sap-com:document:sap:rfc:functions/ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest";

private static final String USERNAME    = "Alex";
private static final String PASSWORD    = "hunter2";
private static final int    TIMEOUT     = 10000;

@Override
protected void onStart() {
    super.onStart();

    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setTextSize(10);

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapObject so_etTopCustomers = new SoapObject(NAMESPACE,
            "ET_TOPCUSTOMERS");
    SoapObject so_item = new SoapObject(NAMESPACE, "item");

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER10); //Also tried VER11 and VER12
    envelope.dotNet = false;

    envelope.setOutputSoapObject(request);

    PropertyInfo kunnr = new PropertyInfo();
    kunnr.setName("KUNNR");
    // kunnr.setValue("");              // Tried with and without these 
    // kunnr.setType(String.class);
    so_item.addProperty(kunnr);

    PropertyInfo name1 = new PropertyInfo();
    name1.setName("NAME1");
    // name1.setValue("");
    // name1.setType(String.class);
    so_item.addProperty(name1);

    PropertyInfo custnum = new PropertyInfo();
    custnum.setName("CUSTNUM");
    // custnum.setValue("");
    // custnum.setType(String.class);
    so_item.addProperty(custnum);

    PropertyInfo netwr = new PropertyInfo();
    netwr.setName("NETWR");
    // netwr.setValue("");
    // netwr.setType(String.class);
    so_item.addProperty(netwr);

    so_etTopCustomers.addSoapObject(so_item);

    request.addSoapObject(so_etTopCustomers);

    PropertyInfo i_toplist = new PropertyInfo();
    i_toplist.setName("I_TOPCUST");
    // i_toplist.setValue("10");
    // i_toplist.setType(String.class);
    request.addProperty(i_toplist);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVICE_URL,
            TIMEOUT);
    androidHttpTransport.debug = true;

    try {
        List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
        headerList.add(new HeaderProperty("Authorization", "Basic "
                + org.kobjects.base64.Base64.encode(USERNAME+":"+PASSWORD
                        .getBytes())));
        headerList
                .add(new HeaderProperty(
                        "SOAPAction",
                        SOAP_ACTION));
        Log.i("Info", "Now trying androidHttpTransport.call()...");
        androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
        SoapObject response = (SoapObject) envelope.getResponse();
    } catch (IOException e) {
        Log.e("IOException", "androidHttpTransport.call()");
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        Log.e("XmlPullParserException", "androidHttpTransport.call()");
        e.printStackTrace();
    }

    txt.setText("Request sent:\n\n" + androidHttpTransport.requestDump
            + "\n\nResponse recieved:\n\n"
            + androidHttpTransport.responseDump);
    Log.d("Request sent", androidHttpTransport.requestDump);
    Log.d("Response recieved", androidHttpTransport.responseDump);

    SoapObject result = null;
    try {
        Log.i("Info", "Now trying envelope.getResponse()...");
        result = (SoapObject) envelope.getResponse();
    } catch (SoapFault e) {
        Log.e("SoapFault", "envelope.getResponse()");
        e.printStackTrace();
    } catch (Exception e) {
        Log.e("Exception", "envelope.getResponse()");
        e.printStackTrace();
    }
}
}

ksoap2方法的对应Logcat:

10-31 10:05:46.670: I/Info(2045): Now trying androidHttpTransport.call()...
10-31 10:05:47.769: E/IOException(2045): androidHttpTransport.call()
10-31 10:05:47.769: W/System.err(2045): SoapFault - faultcode: 'soap-env:Server' faultstring: 'Web service processing error; more details in the web service error log on provider side (UTC timestamp 20121031043547; Transaction ID 701423E22D06F17F9A51001C23D23A48)' faultactor: 'null' detail: org.kxml2.kdom.Node@4123f288
10-31 10:05:47.779: W/System.err(2045):     at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:141)
10-31 10:05:47.790: W/System.err(2045):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
10-31 10:05:47.790: W/System.err(2045):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
10-31 10:05:47.790: W/System.err(2045):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:214)
10-31 10:05:47.809: W/System.err(2045):     at play.ground.soap.SoapRetail.onStart(SoapRetail.java:136)
10-31 10:05:47.809: W/System.err(2045):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-31 10:05:47.809: W/System.err(2045):     at android.app.Activity.performStart(Activity.java:5018)
10-31 10:05:47.819: W/System.err(2045):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
10-31 10:05:47.819: W/System.err(2045):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-31 10:05:47.850: W/System.err(2045):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-31 10:05:47.850: W/System.err(2045):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-31 10:05:47.850: W/System.err(2045):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 10:05:47.850: W/System.err(2045):     at android.os.Looper.loop(Looper.java:137)
10-31 10:05:47.850: W/System.err(2045):     at android.app.ActivityThread.main(ActivityThread.java:4745)
10-31 10:05:47.859: W/System.err(2045):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 10:05:47.859: W/System.err(2045):     at java.lang.reflect.Method.invoke(Method.java:511)
10-31 10:05:47.859: W/System.err(2045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-31 10:05:47.859: W/System.err(2045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-31 10:05:47.859: W/System.err(2045):     at dalvik.system.NativeStart.main(Native Method)
10-31 10:05:47.949: D/Request sent(2045): <v:Envelope xmlns:i="http://www.w3.org/1999/XMLSchema-instance" xmlns:d="http://www.w3.org/1999/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /><v:Body><n0:ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest id="o0" c:root="1" xmlns:n0="urn:sap-com:document:sap:rfc:functions"><n0:ET_TOPCUSTOMERS i:type="n0:ET_TOPCUSTOMERS"><n0:item i:type="n0:item"><KUNNR i:null="true" /><NAME1 i:null="true" /><CUSTNUM i:null="true" /><NETWR i:null="true" /></n0:item></n0:ET_TOPCUSTOMERS><I_TOPCUST i:null="true" /></n0:ZRetail_Exec_TopProducts_WebS/ZRFC_TOPCUSTOMER_NEWRequest></v:Body></v:Envelope>

10-31 10:05:47.966: D/Response recieved(2045): <soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header/><soap-env:Body><soap-env:Fault><faultcode>soap-env:Server</faultcode><faultstring xml:lang="en">Web service processing error; more details in the web service error log on provider side (UTC timestamp 20121031043547; Transaction ID 701423E22D06F17F9A51001C23D23A48)</faultstring><detail/></soap-env:Fault></soap-env:Body></soap-env:Envelope>

SOAPUI生成的请求(在SOAPUI中有效):

POST http://www.icanhazcheezeburger.com/sap/bc/srt/rfc/sap/zrfc_topcustomer_webs/100/zrfc_topcustomer_webs/zrfc_topcustomer_webs HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "urn:sap-com:document:sap:rfc:functions:ZRFC_TOPCUSTOMER_webs:ZRFC_TOPCUSTOMER_NEWRequest"
Authorization: Basic <encoded"Alex:hunter2">=
Content-Length: 596
Host: icanhazcheezeburger.com:22
Proxy-Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:rfc:functions">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:ZRFC_TOPCUSTOMER_NEW>
         <!--Optional:-->
         <ET_TOPCUSTOMERS>
            <!--Zero or more repetitions:-->
            <item>
               <KUNNR/>
               <NAME1/>
               <CUSTNUM/>
               <NETWR/>
            </item>
         </ET_TOPCUSTOMERS>
         <!--Optional:-->
         <I_TOPCUST>10</I_TOPCUST>
      </urn:ZRFC_TOPCUSTOMER_NEW>
   </soapenv:Body>
</soapenv:Envelope>

更新

我可以使用不同的方法使用webservices,即不使用kso​​ap2。我复制了SOAP UI生成的响应并将其发送到HTTP POST请求中。我不得不添加SOAPAction和Auth标头。如果有人需要代码,请告诉我。谢谢大家。我将问题保持打开,因为我无法使用kso​​ap2这样做。

0 个答案:

没有答案