Example of using WebService to verify user logon in Android

Source: Internet
Author: User

I have made two Android projects some time ago. Now I want to take the time to summarize some of these knowledge points, so I can further study them.

Because mobile client development generally involves dealing with servers, user login verification is indispensable in general applications. Therefore, I will use WebService for verification in my previous project.

Written separately. The server side of our mobile app is developed by Asp.net. Therefore, WebService is developed by C # and published on IIS.

The android SDK does not provide a library to call WebService. Therefore, you must use a third-party SDK to call WebService. The WebService library of PC version is very rich, but these are too large for Android. There are some sdks suitable for WebService clients on mobile phones. ksoap2 is commonly used.

Ksoap2 address: http://code.google.com/p/ksoap2-android/

What we use in the project is: ksoap2-android-assembly-2.4-jar-with-dependencies.jar.

After ksoap2 is referenced in the project, we will introduce the following packages:

Import org. ksoap2.soapenvelope;
Import org. ksoap2.serialization. soapobject;
Import org. ksoap2.serialization. soapserializationenvelope;
Import org. ksoap2.transport. httptransportse;

Then we need to write a method to call and verify the user login, and call the WebService method in it. The Code is as follows:

Public String getuserws (string methodname, string [] parameterlist ){
// Create a soapobject object and specify the namespace and Method Name of the WebService.
Soapobject request = new soapobject (config. namespace, methodname );
// If the called function has parameters, you can set the parameters to be passed. Note: The first parameter uses multiple arg0 parameters and so on. arg1, arg2...
If (parameterlist! = NULL ){
// For (INT I = 0; I <parameterlist. length; I ++ ){
Request. addproperty ("key", parameterlist [0]);
Request. addproperty ("username", parameterlist [1]);
Request. addproperty ("password", parameterlist [2]);

//}
}

// Generate the SOAP request information that calls the WebService method, and specify the soap version
Soapserializationenvelope envelope = new soapserializationenvelope (
Soapenvelope. ver11 );
// Envelope. setoutputsoapobject (request );
// The above sentence is equivalent to the following sentence which assigns the soapobject object to the envelope object.
Envelope. bodyout = request;
// Currently,. Net ws does not need to be called here.
Envelope. DOTNET = true;

/*
* Do not use androidhttptransport ht = new androidhttptransport (URL) here );
* This is a class to expire.
* Create an httptransportse object. You can use the httptransportse Class Construction Method to specify the URL of the web service WSDL document.
*/

// Here soap_getuserinfoaction = "http://172.16.xx.xxx: 3366/service/ewineservice. asmx? OP = mobile_getuserinfo ";
Httptransportse ht = new httptransportse (config. soap_getuserinfoaction );
Try {
// Request WS
Ht. Call (config. soap_action, envelope );
If (envelope. getresponse ()! = NULL ){
// Obtain the WS function return value.
// System. Out. println (envelope. getresponse (). tostring ());
Log. D ("Wine", "getuserws Result :"
+ Envelope. getresponse (). tostring ());
Return envelope. getresponse (). tostring ();
}

} Catch (exception e ){
E. printstacktrace ();
System. Out. println (E. getmessage ());
Log. D ("Wine", "getuserws error:" + E. getmessage ());
}
Return NULL;
}

The specific call code is as follows:

// Click the confirmation button to execute
String [] parameterlist = new string [3];
Parameterlist [0] = loginkey;
Parameterlist [1] = txtuser. gettext (). tostring ();
Parameterlist [2] = txtpassword. gettext (). tostring ();

// Note that config. method_getuserinfo is the Chinese name of the specifically called WebService, for example, method_getuserinfo = "mobile_getuserinfo ";
// Call WebService
String strremoteinfo = getuserws (config. method_getuserinfo,
Parameterlist );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.