Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ksoap2 org.xmlpull.v1.xmlpullparserexception expected start_tag error

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.

like image 585
cham Avatar asked Sep 14 '12 18:09

cham


3 Answers

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);
like image 139
DeltaCap019 Avatar answered Nov 14 '22 21:11

DeltaCap019


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/";
like image 36
RTOSkit Avatar answered Nov 14 '22 22:11

RTOSkit


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.

like image 3
Solace Avatar answered Nov 14 '22 21:11

Solace