vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, June 27, 2012

Tutorial to Connect dotnet web service in android application


Introduction:
It’s very interesting to a talk about web service, here I will explain to connect simple type dot net web service using android application. By using a web service we can easily get the desired data by sending request.
Before I start the topic we discuss, what are the files needed to start to access the web service.
Steps to follow:
1.       Make sure you have added the Ksopa2 jar file on your directory, if you need help to add the file refer the below link, download and follow the steps to add ksoap2 jar file.

http://www.dotnetcode.in/2012/06/adding-ksoap2-jar-file-or-using-soap.html

2.       Add the below code on androidManifest.xml file, below code permit the application to connect on intenet. Add the code outside the application tag in androidManifest.xml file

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

In a dotnet web service we must know about these things i.e.

1. Methods name-Here I call the methods name as MobileLogin
2. Namespace-Here I call namespace name as http://mobileapp.com
4. Passing parameter name-here I used usrname and password

So you before workout your project collect all the details about web service.

Use the below code
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
public class webActivity extends Activity {
                /** Called when the activity is first created. */
                HttpTransportSE androidHttpTransport;
                String NAMESPACE = "http://mobileapp.com";
                String METHOD_NAME = "MobileLogin";
                String SOAP_ACTION = "http://mobileapp.com/MobileLogin";
                String URL = "http://132.121.21.77:8080/MobileService.asmx?";
                @Override
                public void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.main);

                                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                                                                SoapEnvelope.VER11);

                                request.addProperty("usrname ", "xxxxx@gmail.com");
                                request.addProperty("password ", "abcdefg");
//**********www.dotnetcode.in*********************
                                envelope.setOutputSoapObject(request);
                                envelope.implicitTypes = true;
                                envelope.dotNet = true;
                                int Timeout1 = 20000;
                                androidHttpTransport = new HttpTransportSE(URL, Timeout1);
                                androidHttpTransport.debug = true;
                                try {
                                                androidHttpTransport.call(SOAP_ACTION, envelope);
                                                Object response = envelope.getResponse();
                                                String rslt;
                                                //Finally you get the result from web service
                                                rslt=response.toString();
                                } catch (Exception e) {
                                                //**********www.dotnetcode.in*********************
                                                // TODO: handle exception
                                }
                }
}


That’s it above code works perfectly …Incase if you need the source code put your commnets with your email id…I will forward the source code.
Happy coding…keep reading

0 comments:

Post a Comment