Steps for Android to access WebService-cxf

Source: Internet
Author: User

1. Add the ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar package to the libs directory of the android Project
2. Web service tool class webservicehelper

Import Java. util. hashmap; import Java. util. map. entry; import Org. ksoap2.soapenvelope; import Org. ksoap2.serialization. soapobject; import Org. ksoap2.serialization. soapserializationenvelope; import Org. ksoap2.transport. httptransportse; import android. annotation. suppresslint; import android. OS. build; import android. OS. strictmode;/*** tool class for accessing Web Service * @ author jcuckoo **/@ suppresslint ("newapi") public class Webservicehelper {static {If (build. version. sdk_int> = build. version_codes.ice_cream_sandwich) {// After 4.0, you must add the following two lines of code to access web servicestrictmode. setthreadpolicy (New strictmode. threadpolicy. builder (). detectdiskreads (). detectdiskwrites (). detectnetwork (). penaltylog (). build (); strictmode. setvmpolicy (New strictmode. vmpolicy. builder (). detectleakedsqlliteobjects (). detectleakedclosableobjects (). penaltylog (). pen Altydeath (). build ());} // The preceding settings are not required for versions earlier than 4.0}/*** @ Param URL Web Service path * @ Param namespace Web Service namespace * @ Param methodname web service method name * @ Param Params web service method parameter */public static soapobject getsoapobject (string servicename, string methodname, string soapaction, hashmap <string, Object> Params) {string url = "http: // 192.168.1.89: 8080/mywebservice/WebService/" + servicename + "? WSDL "; string namespace =" http://webservice.dh.com/"; // namespace, the server-side namespace property value string method_name = methodname; string soap_action = soapaction; soapobject soap = NULL; try {soapobject RPC = new soapobject (namespace, method_name); If (Params! = NULL & Params. size ()> 0) {for (Entry <string, Object> item: Params. entryset () {RPC. addproperty (item. getkey (), item. getvalue (). tostring () ;}} soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); envelope. bodyout = RPC; envelope. DOTNET = false; // true -- net; false -- Java; envelope. setoutputsoapobject (RPC); httptransportse ht = new httptransportse (URL); ht. DEBUG = true; ht. call (soap_action, envelope); try {soap = (soapobject) envelope. getresponse ();} catch (exception e) {soap = (soapobject) envelope. bodyin ;}} catch (exception ex) {ex. printstacktrace ();} return soap ;}}

3. Create a service layer loginservice

Import Java. util. hashmap; import Org. ksoap2.serialization. soapobject; import android. content. context; import android. widget. toast; import COM. DH. util. webservicehelper; public class loginservice {public void login (context, string username, string userpwd) {hashmap <string, Object> paramsmap = new hashmap <string, Object> (); paramsmap. put ("username", username); paramsmap. put ("userpwd", userpwd); // service name Method Name soapaction -- null parameter data soapobject soapojbect = webservicehelper. getsoapobject ("userservice", "checkuser", null, paramsmap); If (soapojbect! = NULL) {toast. maketext (context, soapojbect. getpropertyasstring (0), Toast. length_long). Show ();}}}

4. Design Layout interface activity_main.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: paddingbottom = "@ dimen/activity_vertical_margin" Android: paddingleft = "@ dimen/plugin" Android: paddingright = "@ dimen/plugin" Android: paddingtop = "@ dimen/plugin" tools: context = ". mainactivity "Android: Orientation =" vertical "> <linearlayout Android: Id =" @ + ID/layout_top "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: orientation = "horizontal"> <textview Android: text = "username:" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> <edittext Android: id = "@ + ID/username" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout> <linearlayout Android: id = "@ + ID/layout_middle" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: text = "password: "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "/> <edittext Android: Id =" @ + ID/userpwd "Android: layout_width =" fill_parent "Android: layout_height = "wrap_content"/> </linearlayout> <button Android: Id = "@ + ID/login_button" Android: text = "login" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout>

5. Compile mainactivity on the main interface of the program

Import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. toast; import COM. DH. service. loginservice; public class mainactivity extends activity {private edittext usernameedittext; private edittext userpwdedittext; private button loginbutton; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); usernameedittext = (edittext) findviewbyid (R. id. username); userpwdedittext = (edittext) findviewbyid (R. id. userpwd); loginbutton = (button) findviewbyid (R. id. login_button); loginbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view arg0) {string username = usernameedittext. gettext (). tostring (); string userpwd = userpwdedittext. gettext (). tostring (); If ("". equals (username )&&"". equals (userpwd) {toast. maketext (mainactivity. this, "the user name or password cannot be blank", toast. length_long ). show ();} loginservice = new loginservice (); loginservice. login (getapplicationcontext (), username, userpwd );}});}}

6. Set network access permissions in androidmanifest. xml

<! -- Set permissions for using web services --> <uses-Permission Android: Name = "android. Permission. Internet"/>

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.