Thursday, 13 June 2013

Consume .net webservice in android using ksoap2



The ksoap2-android project provides a lightweight and efficient SOAP client library for the Android platform.
Check this link http://code.google.com/p/ksoap2-android/
1)First download ksoap2 jar file and add that file to libs folder after that right click the ksoap file and  click on add as library option.
2)add namespaces


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
3)declare the variables for accessing webservice
 private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";

     
  private static final String METHOD_NAME = "HelloWorld";
        private static final String NAMESPACE = "http://tempuri.org/";
        private static final String URL = "http://10.0.2.2:4718/SampleWebservice.asmx";
       TextView webserviceTextView;
       Object response;

Here  SOAP_ACTION  is the namespace of webservice.
METHOD_NAME   is the name of method created in the webservice . here its HelloWorld.
NAMESPACE  is target namespace of .asmx file.
URL  is url of .asmx file.
4) Add Internet permission to Androidanifest.xml file.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />



5)Here we need to create a class object extending the AsyncTask class as shown below..


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        webserviceTextView=(TextView)(findViewById(R.id.textView1));
        new Dowork().execute();
    }
    public class Dowork extends AsyncTask<String,Object,Object>
    {
        private final ProgressDialog dialogue =new ProgressDialog(second.this);
        @Override
        protected void onPreExecute()
        {
            this.dialogue.setMessage("Checking..");
            this.dialogue.show();
        }
        @Override
        protected void onCancelled(Object result)
        {
            super.onCancelled();
        }
        @Override
        protected void onPostExecute(Object result)
        {
            if(result!=null)
            {
                second.this.webserviceTextView.setText(result.toString());
            }
            else
            {
                Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_LONG).show();
            }
            if (this.dialogue.isShowing()) {

                this.dialogue.dismiss();
            }
        }
        @Override
        protected Object doInBackground(String ...params)
        {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);
            try
            {
                AndroidHttpTransport aht=new AndroidHttpTransport(URL);
                aht.call(SOAP_ACTION,envelope);
                response=(SoapPrimitive)envelope.getResponse();
                return response;
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
            return response;
        }

    }


6)this DoWork class will return the webservice response. Here in AsyncTask<String,Object,Object>
First parameter string is passed to the doInBackground() function as an array.
The second parameter is used fpr progress updates.
The third parameter is for the results. the doInBackground() return type must match the third parameter data type.
1)      Next we may want to show the progress of our task on the screen.  In this example we will use a ProgressDialog.  We will need to use the onPreExecute() method.
2)      For return to my Activity and drop off what the web search found.  Using the onPostExecute() function will can do that.
3)      Here doIn background will return response and we can perform any operation based on that result in onPostExecute () method. Here I am setting response to a textview and it will display the response from .net webservice.



5 comments:

  1. Have tried all the steps that you have defined. It doesnt give any error but also do not display any result.
    It only executes onpreexecute() method after that app becomes stopped.
    Any suggetions?

    ReplyDelete
  2. thanks Works for me..
    use
    HttpTransportSE aht=new HttpTransportSE (URL);

    instead of AndroidHttpTransport aht=new AndroidHttpTransport(URL);

    use SoapObject response; instead of Object response

    ReplyDelete
  3. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

    ReplyDelete