In this blog I am going to explain How to call a .Net xml webservice in android with the help of an login WebService.
Host: shidhin.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://shidhin.net/Login"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Login xmlns="http://shidhin.net/">
<Email>string</Email>
<Password>string</Password>
</Login>
</soap:Body>
</soap:Envelope>
In this WebService request Our Function name is "Login " and the parameters we are passing are "Email" which is "string" format and "Password" which is also in "string" format.
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<LoginResponse xmlns="http://shidhin.net/">
<LoginResult>
<IsValidLogin>boolean</IsValidLogin>
<ErrorOnFailure>string</ErrorOnFailure>
<Token>string</Token>
</LoginResult>
</LoginResponse>
</soap:Body>
</soap:Envelope>
private static String URL = "http://shidhints.com/Service.asmx?wsdl";
public LoginToken login(String Email, String Password ) throws Exception {
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://shidhin.net/Login";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Email", Email); // Adding Email to request
Request.addProperty("Password", Password ); // Adding Password to request
LoginResult loginResult;
try
{
SoapObject response = executeRequest(Request, SOAP_ACTION);
if(response != null){
loginResult= new LoginResult((SoapObject) response.getProperty(0));
return loginResult;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
// This function is to call the webservice
private SoapObject executeRequest(SoapObject Request, String SOAP_ACTION) throws IOException{
System.setProperty("http.keepAlive", "false");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.implicitTypes=true;
envelope.dotNet = true;
envelope.setAddAdornments(false);
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
envelope.enc = SoapSerializationEnvelope.ENC;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
SoapObject response;
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject) envelope.bodyIn;
return response;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
Here I am executing the request through "executeRequest" function. The result will be in "SoapObject" format. Now I will pass this result to my "LoginResult " class. In that class It will parse the response.
Boolean LoginStatus= false;
String Token = "" , ErrorOnFailure = "";
ValidLogin = response.getProperty("IsValidLogin").toString();
if(ValidLogin.equalsIgnoreCase("true")){
LoginStatus=true;
Token = response.getProperty("Token").toString();
}else{
ErrorOnFailure = response.getProperty("ErrorOnFailure").toString();
}
}
XML Request
POST /Service.asmx HTTP/1.1Host: shidhin.net
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://shidhin.net/Login"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Login xmlns="http://shidhin.net/">
<Email>string</Email>
<Password>string</Password>
</Login>
</soap:Body>
</soap:Envelope>
In this WebService request Our Function name is "Login " and the parameters we are passing are "Email" which is "string" format and "Password" which is also in "string" format.
XML Responc
HTTP/1.1 200 OKContent-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<LoginResponse xmlns="http://shidhin.net/">
<LoginResult>
<IsValidLogin>boolean</IsValidLogin>
<ErrorOnFailure>string</ErrorOnFailure>
<Token>string</Token>
</LoginResult>
</LoginResponse>
</soap:Body>
</soap:Envelope>
In this WebService response we are getting result through the parameters "IsValidLogin" which is a "boolean" value and "ErrorOnFailure" which is a "string" value and "Token" which is also a "string" value. Here if the "IsValidLogin" is true then it will pass the "Token" otherwise it will pass "ErrorOnFailure" which contains the reason of failure.
Android Code
In Android I am calling this WebService with the help of a Library called Ksoap. You can get more about Ksoap form Here. First we need to download the Ksoap .Jar file and add that library to your Android project.Java Code
private static String NAMESPACE = "http://shidhin.net/";private static String URL = "http://shidhints.com/Service.asmx?wsdl";
public LoginToken login(String Email, String Password ) throws Exception {
String METHOD_NAME = "Login";
String SOAP_ACTION = "http://shidhin.net/Login";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("Email", Email); // Adding Email to request
Request.addProperty("Password", Password ); // Adding Password to request
LoginResult loginResult;
try
{
SoapObject response = executeRequest(Request, SOAP_ACTION);
if(response != null){
loginResult= new LoginResult((SoapObject) response.getProperty(0));
return loginResult;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
// This function is to call the webservice
private SoapObject executeRequest(SoapObject Request, String SOAP_ACTION) throws IOException{
System.setProperty("http.keepAlive", "false");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.implicitTypes=true;
envelope.dotNet = true;
envelope.setAddAdornments(false);
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
envelope.enc = SoapSerializationEnvelope.ENC;
envelope.xsd = SoapEnvelope.XSD;
envelope.xsi = SoapEnvelope.XSI;
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
SoapObject response;
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject) envelope.bodyIn;
return response;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
Here I am executing the request through "executeRequest" function. The result will be in "SoapObject" format. Now I will pass this result to my "LoginResult " class. In that class It will parse the response.
Parsing SoapObject
public LoginResult (SoapObject response){Boolean LoginStatus= false;
String Token = "" , ErrorOnFailure = "";
ValidLogin = response.getProperty("IsValidLogin").toString();
if(ValidLogin.equalsIgnoreCase("true")){
LoginStatus=true;
Token = response.getProperty("Token").toString();
}else{
ErrorOnFailure = response.getProperty("ErrorOnFailure").toString();
}
}