4

I have to connect a SAP web service. Here is my code but it warns:

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @1:7 in java.io.InputStreamReader@40e3fe98)

Here is my full code to connect with username and password.

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; 
} 
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
GK Soft
  • 103
  • 2
  • 12
  • I used this post to achive but didnt solved http://stackoverflow.com/questions/5613675/how-to-set-soap-header-using-ksoap2-android – GK Soft Apr 25 '12 at 13:28
  • and also when i set URL as my WSDL url, i get only anyType{} result – GK Soft Apr 25 '12 at 14:08
  • and also when i replace Object response.. line SoapObject response = (SoapObject)soaSeria.. i got ZmblHucremalzemelistesiResponse{THcrmlzlist=anyType{}; } result :S – GK Soft Apr 25 '12 at 14:17
  • ,I had same problem and i find its solution here https://stackoverflow.com/questions/51255957/soap-request-creation-using-ksoap2-for-multilevel-tags/51340332#51340332 you can follow my whole code and get solution.. – Vishwa Pratap Jul 18 '18 at 15:48

1 Answers1

2

Try this:

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();
        }
    }

And AuthTransportSE class is:

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());
        }
    }
}
Oguz Ozkeroglu
  • 3,025
  • 3
  • 38
  • 64
  • Thanks for reply, i tried both of bodyIn and getResponse() but, i got result : ZmblHucremalzemelistesiResponse{THcrmlzlist=anyType{}; } Still not solved :( – GK Soft Apr 25 '12 at 14:33
  • 1
    if you can see this: ZmblHucremalzemelistesiResponse{THcrmlzlist=anyType{}; } you can now access the webservice. Did you try test this service using a SOAP client program with same parameters? – Oguz Ozkeroglu Apr 25 '12 at 14:37
  • here is the last detailed error : SoapFault - faultcode: 'soap-env:Server' faultstring: 'CX_ST_MATCH_ELEMENT:XSLT Exception aufgetreten.Element 'ILgort' erwartet' faultactor: 'null' detail: org.kxml2.kdom.Node@40e845f8 – GK Soft Apr 25 '12 at 15:13