Skip to main content

How to call a webservice in android.

            In this blog I am going to explain How to call a .Net xml webservice in android with the help of an login WebService.

XML Request

POST /Service.asmx HTTP/1.1
Host: shidhints.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://shidhints.com/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://shidhints.com/">
        <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 OK
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://shidhints.com/">
         <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  "ErrorOnFailurewhich is a "string" value and  "Tokenwhich 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://shidhints.com/";
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://shidhints.com/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();
     } 

}



Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …