Advertisemnet

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 http://androidplusiphone.blogspot.com/2012/03/android-chapter-4-web-services-in.html 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. http://code.google.com/p/ksoap2-android/source/browse/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/2.5.2/ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar?r=498

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.

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

 Now I go to Login.java 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 = nhttp://tempuri.org/memberValidation";
private static final String METHOD_NAME = nmemberValidationn;
private static final String MAMESPACE = "http://tempuri.orgr;
private static final String URL = nhttp://10.0.2.2:8833/WebService/Service.asmx";

EditText name;
EditText pass;
TextView tv;
 
@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Login);
name.(EditText)findViewById(R.id.txtname);
pass.(EditText)findViewById(R.id.txtpassword);
tv.(TextView)findViewById(R.id.txtError);
Button btnSignIn.(Button)findViewById(R.id.htnsignin);
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;
soapEnvelop.setOutputSoapObject(Request);
HttpTransportSE htp = new HttpTransportSE(URL);
try{
tv.setText("");
htp.call(SOAPACTION,soapEnvelop);
SoapPrimitive resultString = (SoapPrimitive)soapEnvelop.getResponse();
String id = resultString.toString();
Intent intent = new Intent(Login.this,Welcome.class);
startActivity(intent);
catch 
(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.

SOAP_ACTION -

METHOD_NAME - your method name (" memberValidation ")

NAMESPACE - "http://tempuri.org/"

URL - replacing 'localhost' by '10.0.2.2' .

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.

11 comments:

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

    ReplyDelete
    Replies
    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.

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

    ReplyDelete
    Replies
    1. yeah , I think there is an exception in your program. Did you add the activity to your manifest file ?
      see here http://1.bp.blogspot.com/-TGhz6ealNxw/T3HGYLDUBNI/AAAAAAAAANA/XAZ_aM-Qi7M/s1600/16.png

      Delete
  3. im trying to access real live website with web services, do I need to add the port in the URI or just http://website.com/ws/webservis.asmx

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

      Delete
  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

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

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

      Delete
  5. Hi,

    How do you make the logout work?

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

    ReplyDelete