13

Below is my code, which I have written to validate user log in credentials. The web service written using .net

private static final String SOAP_ACTION = "http://tempuri.org/getCredentials";
private static final String OPERATION_NAME = "getCredentials";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://myStaticIP:portNo/WebSiteName/CommunicationInterface.asmx"; 

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
request.addProperty("username",Username);
request.addProperty("password", Password);

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

try
{
    httptransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
String value = result.toString();
value_LoginWS = value;
val = value;
login_status = Boolean.valueOf(result.toString());

Log.v("CS return value: -", result.toString());
return value;
}
catch (Exception e) 
{
     Log.v("Exception Soap" , e.toString());
}



In line "httptransport.call(SOAP_ACTION, envelope)" I get the exception saying

"org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>
@1:7 in java.io.InputStreamReader@41afb3f0)" <br/><br/>

I have no idea what the error is about. This piece of code is worked perfectly for emulator( changing the staticIP to 10.0.2.2:portNo).

Please help me to solve this problem.

Thank you.

DeltaCap019
  • 6,258
  • 2
  • 42
  • 66
cham
  • 609
  • 3
  • 9
  • 19
  • 2
    @zapl: what do you mean by that friend..... – cham Sep 14 '12 at 19:49
  • I had the same error as this, and found it had nothing to do with my code. If you're running on the emulator, restart your working environment and try again. You may have to repeat this a few times, but it worked for me eventually! – Vivek87799 Mar 07 '13 at 10:05

4 Answers4

61

Below solution is tested and used for WCF Web Services

If you are getting this error

org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <html>
@1:7 in java.io.InputStreamReader@41afb3f0)"

Then the possible chances are that your code is not able to access the web service as it has not been provided with correct values of

METHOD_NAME="";
NAMESPACE ="";      
SOAP_ACTION ="";
URL ="";

I struggled a lot to find these basic but important values to run ksoap2

METHOD_NAME="";
NAMESPACE ="";      
SOAP_ACTION ="";
URL ="";

There were various examples which actualy told me the theory behind this value thats how to generate them like wise SOAP_ACTION = NAMESPACE + METHOD_NAME.

And I kept on experimenting with various combinations with no Result.

A guy like me who is having little experience with WebServices and Ksoap2 and had woirked with JSON most of the time actually get frustated, what the heck these values are and how to get them correctly.

You will never ever face difficulty in finding out these values after going thru the below procedure.

Run your WebService

1. WebService

It will show you a window like this to you.

Picture 1 webservice

2. WSDL

Now Open Its WSDL File by clicking on the link marked in the pick to look at its WSDL

It will something look like this.

picture 2 wsdl

3.To get Namespace for the WebService

Now search for string "Namespace" using Ctrl+F

You will get something like this

picture 3 namespace

Here you will notice that we have two namespaces

targetNamespace="http://tempuri.org/">
<wsdl:import namespace="iscservices.dedicated.co.za"

now which one to consider we will find out later-on in the following steps

Now which one to use you will find out later

4. To get Method Name and its Corresponding SoapAction

Look for your method you want to access "PutRecipeImagesPost" in this case

Picture 4 Soap Action

You will see there is SOAP Action also here for this method.

As in Soap action is NameSpace + Methodname and here we can see its using "iscservices.dedicated.co.za" so the same we will finalize as our namespace

5. To get URL

Look for the string "soap:address location"

see the picture below

The value of this attribute will be your URL

So eventually we get all our required values.

values according to our example

METHOD_NAME="PutRecipeImagesPost";
NAMESPACE ="iscservices.dedicated.co.za";       
SOAP_ACTION ="iscservices.dedicated.co.za/InterfaceiPhysioIntelWCFService/PutRecipeImagesPost";
URL ="http://10.0.2.2:51809/iPhysioIntelService.svc/second/";

If you are not able to see the above snapshots or not able to get the values for these in you WSDl then tell the WebService deveoper to fix that up.

Later on how to use KSoap2

see the below snippet

SoapObject req = new SoapObject(NAMESPACE,METHOD_NAME);
            //SoapObject req = new SoapObject(Namespace_Server,MethodName_Server);

    //req.addProperty(KEY, VALUE);
//Key : - parameter name that is there in URL of webservice
//value:- value we want to send to the parameter
    req.addProperty("ImageData", data);
    req.addProperty("login", CommonStaticData.getusername());
    req.addProperty("password",CommonStaticData.getpassword());
    req.addProperty("recipeId",FileID);

    MarshalBase64 mbase = new MarshalBase64();// marshal is used to serialize the byte array

    SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelop.bodyOut = req;
    envelop.encodingStyle = SoapSerializationEnvelope.ENC2001;
    envelop.dotNet = true;
    envelop.setOutputSoapObject(req);


    HttpTransportSE aht = new HttpTransportSE(URL);

    mbase.register(envelop);


    aht.call(SOAP_ACTION,envelop);
DeltaCap019
  • 6,258
  • 2
  • 42
  • 66
  • @AZone I need help regarding to SOAP. – Piyush May 28 '14 at 09:05
  • @PiYusHGuPtA ok what kind of help. I am not pro in that. once I had implemented it in one project. but still I would like to know what type of problem you are facing in SOAP – DeltaCap019 May 28 '14 at 11:11
  • @AZone I have already googled and do lots of search and i have also checked all thignd related to it but all is perfect i have used. – Piyush May 28 '14 at 11:18
  • @AZone can you elaborate for it now? – Piyush May 28 '14 at 11:25
  • How can I find out if my web service is a WCF web service? – Solace Jun 29 '15 at 02:15
  • 2
    @Solace to my knowledge they have end point with extension `.asmx` – DeltaCap019 Jun 29 '15 at 06:06
  • 2
    @AZone Thank you for the information. Mine does not end with `.asmx` but the information in your answer was still very useful. But I checked everything and everything was according to your answer in my client program, and was still getting the error. Then I realized that I was using `SoapEnvelope.VER12` which was the wrong version, changed it to `SoapEnvelope.VER11` and it started working. So I wrote an answer in which I pointed to this answer first and then added my cent. – Solace Jun 29 '15 at 15:32
  • This answer is really helpful. Thank you! – merve bıçakçı Mar 07 '16 at 00:28
  • @NullnVoid I have been searching for this for 2 whole days now and thanks for the amazing answer. But the problem is I have been using MSXML to create the request and getting a blank response – Anubrata Santra Sep 09 '16 at 11:44
  • @NullnVoid I have done exactly what you have written. but still i am unable to remove the error – Moeez Feb 10 '17 at 11:23
  • You saved my life, dude! – Oleksii Shliama Dec 09 '17 at 15:23
  • Thanks. In order to find `soap action` instead of `PutRecipeImagesPost` search a word inside a tag of the first line () or `soapbind:operation`. An URL is contained in ``. – CoolMind Sep 25 '18 at 12:22
6

The answer from @Azone is variously corect, but really, if ksoap returns a reply as

expected START_TAG { ... } Envelope (position:START_TAG <{ ... } ...

refers to need something different from what it use.

So you need to use the same namespace source for START_TAG "soapenv"

the ksoap2 library uses the default namespace

 xmlns:v="http://www.w3.org/2003/05/soap-envelope" 

so your server should maintain the same source

tornado-webservices example:

self._envelope.setAttribute ('xmlns:soapenv', 'http://www.w3.org/2003/05/soap-envelope')

or the other way you need configure ksoap as your server

server:

self._envelope.setAttribute ('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/') 

ksoap:

envelope.env = "http://schemas.xmlsoap.org/soap/envelope/";
RTOSkit
  • 1,161
  • 1
  • 11
  • 23
  • After I add `envelope.env = "http://schemas.xmlsoap.org/soap/envelope/";` I still get the `expected START_TAG...` error... – bbesase Jul 31 '14 at 14:01
  • 1
    @bbesase you should configure the ksoap "envelope" with the same "envelope" you use on your server, no copying and pasting my data examples. If you do not have access to your server, use the soapUI tool to determine your "envelope" – RTOSkit Aug 14 '14 at 07:57
  • Using wireshark and some curl'ing to the device showed me that the default soapenv (xmlsoap) was incorrect and I needed to set it (to w3.org) as you've shown. Thanks! – Alain Collins Sep 27 '14 at 19:13
3

If you have used the values of NAMESPACE, URL, SOAP_ACTION, and METHOD_NAME as described in Azone's answer but still getting the error, check the version of SOAP in this statement:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VERXX);

In my case, I had used the SoapEnvelope.VER12, and when I changed it back to SoapEnvelope.VER11, the error was resolved.

How to find the SOAP Version from WSDL:

Quoting from this answer:

SOAP 1.1 uses namespace http://schemas.xmlsoap.org/wsdl/soap/

SOAP 1.2 uses namespace http://schemas.xmlsoap.org/wsdl/soap12/

The wsdl is able to define operations under soap 1.1 and soap 1.2 at the same time in the same wsdl. Thats useful if you need to evolve your wsdl to support new functionality that requires soap 1.2 (eg. MTOM), in this case you dont need to create a new service but just evolve the original one.

Community
  • 1
  • 1
Solace
  • 7,868
  • 17
  • 74
  • 162
1

The answer to this question as I figure it out is, in the SOAP_ADDRESS string, remove the /WebSiteName part and it works fine. I have tested this in my android device. Works perfectly.

The corrected String should be as follow:

private static final String SOAP_ADDRESS = "http://myStaticIP:portNo/CommunicationInterface.asmx";

where CommunicationInterface.asmx is the web service name. other static variables remain same.

Note: This is used when the web site is hosted in IIS.

cham
  • 609
  • 3
  • 9
  • 19
  • I have declared it like this `private static final String URL = "http://my_system_ip:3908/WebService.asmx?";` but still unable to remove the error – Moeez Feb 11 '17 at 06:09