2012-04-25 117 views
4

我必須連接SAP Web服務。這裏是我的代碼,但它警告:org.xmlpull.v1.XmlPullParserException:expected:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope(position:START_TAG @ 1:7 in java.io.InputStreamReader @ 40e3fe98)如何使用kso​​ap2驗證SAP Web服務

這裏是我完整的代碼連接用戶名和密碼。 感謝您的回覆。

final static String NAMESPACE = "urn:sap-com:document:sap:soap:functions:mc-style"; 
final static String METHOD_NAME = "ZmblHucremalzemelistesi"; 
final static String SOAP_ACTION = ""; 
final static String URL = "http://xxx.xxx.xxx.xxx:1080/sap/bc/srt/wsdl/bndg_4F969242EA785040E10080008D0B0B03/wsdl11/allinone/ws_policy/document?sap-client=010"; 


private void testWS() { 
    // TODO Auto-generated method stub 


    SoapObject reSoapObject = new SoapObject(NAMESPACE, METHOD_NAME); 
    SoapSerializationEnvelope soaSerializationEnvelope = new SoapSerializationEnvelope(
      SoapEnvelope.VER11); 

    reSoapObject.addProperty("ILgort", "H12"); 

    soaSerializationEnvelope.setOutputSoapObject(reSoapObject); 
    soaSerializationEnvelope.headerOut = new Element[1]; 
    soaSerializationEnvelope.headerOut[0] = buildAuthHeader(); 

    HttpTransportSE httpTransportSE = new HttpTransportSE(URL); 

    try { 

     httpTransportSE.call(SOAP_ACTION, soaSerializationEnvelope); 

     Object response = soaSerializationEnvelope.getResponse(); 

     tv.setText(response.toString()); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } 


} 

private Element buildAuthHeader() { 

    Element h = new Element().createElement(NAMESPACE, "AuthHeader"); 
    Element username = new Element().createElement(NAMESPACE, "user"); 
    username.addChild(Node.TEXT, "testuser"); 
    h.addChild(Node.ELEMENT, username); 
    Element pass = new Element().createElement(NAMESPACE, "pass"); 
    pass.addChild(Node.TEXT, "testpwd"); 
    h.addChild(Node.ELEMENT, pass); 

    return h; 
} 
+0

我用這個帖子才達到但沒有解決http://stackoverflow.com/questions/5613675/how -to-設置皁集管使用-ksoap2-機器人 – 2012-04-25 13:28:02

+0

並且當我設置URL作爲我的WSDL URL,我只得到anyType的{}導致 – 2012-04-25 14:08:09

+0

並且還當我更換對象響應..線SoapObject響應=(SoapObject) SOA Seria ..我得到了ZmblHucremalzemelistesiResponse {THcrmlzlist = anyType {}; }結果:S – 2012-04-25 14:17:37

回答

2

試試這個:

final static String NAMESPACE = "urn:sap-com:document:sap:soap:functions:mc-style"; 
    final static String METHOD_NAME = "ZmblHucremalzemelistesi"; 
    final static String SOAP_ACTION = ""; 
    final static String URL = "http://xxx.xxx.xxx.xxx:1080/sap/bc/srt/wsdl/bndg_4F969242EA785040E10080008D0B0B03/wsdl11/allinone/ws_policy/document?sap-client=010"; 

    private static final String USERNAME = "YOUR_USERNAME"; 
    private static final String PASSWORD = "YOUR_PASSWORD"; 

    private void testWS() { 
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty("ILgort", "H12"); 

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

     AuthTransportSE androidHttpTransport = new AuthTransportSE(URL, USERNAME, PASSWORD); 
     androidHttpTransport.debug = true; 

     try { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject) envelope.getResponse(); 
      // if it did not work, try this: 
      // SoapObject response = (SoapObject) envelope.bodyIn; 

      tv.setText(response.toString()); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

而且AuthTransportSE類是:

import java.io.IOException; 

import org.ksoap2.transport.HttpTransportSE; 
import org.ksoap2.transport.ServiceConnection; 
import org.ksoap2.transport.ServiceConnectionSE; 

public class AuthTransportSE extends HttpTransportSE{ 
    private String username; 
    private String password; 

    public AuthTransportSE(String url, String username, String password) { 
     super(url); 
     this.username = username; 
     this.password = password;  
    } 

    protected ServiceConnection getServiceConnection() throws IOException { 
     ServiceConnection midpConnection = new ServiceConnectionSE(url); 
     addBasicAuthentication(midpConnection); 
     return midpConnection; 
    } 

    protected void addBasicAuthentication(ServiceConnection midpConnection) throws IOException { 
     if (username != null && password != null) { 
      StringBuffer buf = new StringBuffer(username); 
      buf.append(':').append(password); 
      byte[] raw = buf.toString().getBytes(); 
      buf.setLength(0); 
      buf.append("Basic "); 
      org.kobjects.base64.Base64.encode(raw, 0, raw.length, buf); 
      midpConnection.setRequestProperty("Authorization", buf.toString()); 
     } 
    } 
} 
+0

感謝答覆,我試圖既bodyIn和GETRESPONSE(的),但是,我得到的結果:ZmblHucremalzemelistesiResponse {THcrmlzlist = anyType的{}; } 仍然沒有解決:( – 2012-04-25 14:33:11

+1

,如果你可以看到這個:ZmblHucremalzemelistesiResponse {THcrmlzlist = anyType {};}你現在可以訪問web服務你有沒有嘗試使用具有相同參數的SOAP客戶端程序來測試此服務 – OguzOzkeroglu 2012-04-25 14:37:16

+0

是的,沒有與WSNavigator沒有問題 – 2012-04-25 14:47:00

相關問題