ksoap2 org.xmlpull.v1.xmlpullparserexception expected start_tag error

cham picture cham · Sep 14, 2012 · Viewed 34.8k times · Source

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.

Answer

DeltaCap019 picture DeltaCap019 · Jun 6, 2013

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