Earn Extra Money At Home. Starting from Now!!!

Tuesday, March 27, 2012

Android Chapter 5 : Web Services in Android (WCF) Part 2

Hello everyone, welcome back to Web Services in Android (WCF). This is Android Chapter 4 : Web Services in Android (WCF) Part 2. I'm Mihiran Rupasinghe. In previous chapter we looked at how to create a web service in ASP.NET with SQL database.  Now lets see how to connect our android application with our web service.

Since I'm using "SoapSerializationEnvelop" to use web services in my app, I must have a special jar file called "ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar". you can download it from here. Go to this link and click "View raw file". Then you can download the file.

Then copy the file in to this directory. "C:\Program Files\Android\android-sdk\platforms\android-10". Now you are done with that.

Now go to the android project in eclipse. Right click the project and go to properties. Select "Java Build Path", select "Libraries", click "Add External JARs", select the .jar file and press ok.

Ok. Now you have to give INTERNET permission for your app. To do that, in your manifest file write the uses-permission tag like this at bottom.

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

 Now I go to file which is going to access remote database. Import these headers.

 import org.ksoap2.*;
 import org.ksoap2.serialization.*;
 import org.ksoap2.transport.*;

Then I modify the file like below. In here, to connect to the web server I'm using "SoapSerializationEnvelop" and "HttpTransportSE" classes.

public class Login extends Activity {

private static final String SOAP_ACTION = n";
private static final String METHOD_NAME = nmemberValidationn;
private static final String MAMESPACE = "http://tempuri.orgr;
private static final String URL = nhttp://";

EditText name;
EditText pass;
TextView tv;
protected void onCreate(Bundle savedInstanceState) 
Button btnSignIn.(Button)findViewById(;
btnSignIn.setOnClickListener(new OnClickListener() {
public void onClick(View v) 
SoapObject Request = new SoapObject(NAMESPACE, METHOD NAME);
Request.addProperty("name" , name.getText().toString());
Request.addProperty("passwd", pass.getText().toString());
SoapSerializationEnvelope soapEnvelop;
soapEnvelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelop.dotNet = true;
HttpTransportSE htp = new HttpTransportSE(URL);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelop.getResponse();
String id = resultString.toString();
Intent intent = new Intent(Login.this,Welcome.class);
(Exception e){
tv.setText("Invalid details. Please check again!");

This is the most important part.
The four string in above are defined according to your web service web page.


METHOD_NAME - your method name (" memberValidation ")


URL - replacing 'localhost' by '' .

And also, I'm using a textview for error message. If user is not valid it gives an error message.

Important :
The first parameter name of Request.addProperty method should be equal to parameter name in memberValidation method. Otherwise it won't work.

If everything is fine user redirect to the welcome page.
Run it in emulator and see how nice it is.
Make sure your web service is running..

Congratulation !
We are done with the web services in our app.

In my next chapter I'll explain how to pass a value from one activity to another.
See you in the next chapter.


  1. First, this very good. But, String Id is not user in you code, can you explain plz.

    1. yeah! In user log into the app it checks the id is available. If it is not available an exception will be occurred in this line

      'String id = resultString.tostring();'

      Then it jumps to catch block and display an error message. If id is available (user is there) then it'll redirect to welcome page.

  2. my app is not working... its saying unfortunately, solutionDude has stopped working.. can u plz guide me what actually is causing this problem...

    1. yeah , I think there is an exception in your program. Did you add the activity to your manifest file ?
      see here

  3. im trying to access real live website with web services, do I need to add the port in the URI or just

    1. No. you don't need the port number for real live websites. It'll be needed only when you run it using localhost.

  4. Hi Mihiran Rupasinghe,
    I follow your tutorial to test my own app but it appears an error at "btnButton.setOnClickListener(new OnClickListener() {" .Would you like to tell me why? Thank in advance

    1. can you please tell me what is the error message ?

    2. can you please post your code inside "btnButton.setOnClickListener(new OnClickListener() { " ?

  5. Hi,

    How do you make the logout work?

  6. And alsow, when i log inn, and if i paste wrong username or password, the intent still runs.