简体   繁体   中英

Consuming asp.net XML web service in android using ksoap2

I am trying to consume an ASP.NET web service in Android with Ksoap2. Web service returns an XML response in browser like the following:

<?xml version="1.0" encoding="UTF-8"?>
  -<Students>
    -<student>
       <stdName>Maria</stdName>
        <stdRollNo>1</stdRollNo>
    -</student>
  -<student>
       <stdName>Anna</stdName>
        <stdRollNo>2</stdRollNo>
   -</student>
  -</Students>

On calling this service in Android with piece of code given below I get an exception:

Object response=null
try {
    httpTransport.call(soap_action,envelope);
    response=envelope.bodyIn;
    Log.v("response is ", response.toString());
} catch(Exception e) { 
    e.getMessage();
}

Exception:

response: "SoapFault" -faultcode:'soap:server' faultstrings: 'Server was not able to process request' object reference was not set to instance of an object"

I have made changes to that piece of code based on different suggestions given on Stackoverflow

SoapObject response=null
try{
    httpTransport.call(soap_action,envelope);
    response=(SoapObject)envelope.bodyIn;
    Log.v("response is ", response.toString());
} catch(Exception e){
    e.getMessage();
}

and

SoapObject response=null
try{
    httpTransport.call(soap_action,envelope);
    response=(SoapObject)envelope.getresponse();
    Log.v("response is ", response.toString());
} catch(Exception e){
    e.getMessage();
}

But the exception then changes to:

java.lang.classCastexception

Any ideas as to what's happening?

You need to : 1/ Specify your method name (defined in your ASP.Net code) : webMethName. 2/ Use SoapPrimitive instead of SoapObject, it's where you'll get the web service respone.

Please edit your code block with the following :

    String resText = "";
    try {
        // Invoke web service
        httpTransport.call(soap_action + webMethName, envelope);
        // Get the response
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
        // Assign it to resText variable                        
        resText = response.toString();
    }
    catch (Exception e) {
        //Print error
        e.printStackTrace();
        //Assign error message to resText 
        resText = "Error occured";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM