Android calls. Net WebService

Source: Internet
Author: User
Package COM. fairyeye. simple; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. textview; import android. widget. toast; import Org. ksoap2.soapenvelope; import Org. ksoap2.serialization. soapobject; import Org. ksoap2.serialization. Soapserializationenvelope; import Org. ksoap2.transport. httptransportse; public class minisystemactivity extends activity {/** called when the activity is first created. */private edittext username; private edittext userpass; private button btnlogin; private button btnclose; private textview TV; final string soap_action = "http://tempuri.org/HelloWorld"; Private Static final string method_name =" Helloworld "; Private Static final string namespace =" http://tempuri.org/"; Private Static final string url =" http: // 10.0.2.2/webser/webtest. asmx "; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnlogin = (button) findviewbyid (R. id. btnlogin); btnclose = (button) findviewbyid (R. id. btnclose); username = (edittext) find Viewbyid (R. id. username); userpass = (edittext) findviewbyid (R. id. userpass); TV = (textview1_findviewbyid(r.id.txt view); btnlogin. setonclicklistener (listener); Call ();} private onclicklistener listener = new onclicklistener () {public void onclick (view v) {// todo auto-generated method stub if (username. gettext (). tostring (). trim (). equals ("222") & userpass. gettext (). tostring (). trim (). equals ("222 ")) {Intent = new intent (); intent. setclass (minisystemactivity. this, testactivity. class); startactivity (intent);} else {toast. maketext (minisystemactivity. this, "the user or password is incorrect! ", Toast. length_long ). show () ;}}; public void call () {try {soapobject request = new soapobject (namespace, method_name); // request. addproperty ("passonstring", "rajapandian"); // This is the soapserializationenvelope envelope = new soapserializationenvelope (soapenvelope. ver11); envelope. DOTNET = true; envelope. setoutputsoapobject (request); httptransportse androidhttptransport = new httptransportse (URL); androidhttptransport. call (soap_action, envelope); object result = (object) envelope. getresponse (); TV. settext (result. tostring ();} catch (exception e) {system. out. println (E. getmessage (); TV. settext (E. getmessage ());}}}

------------------------------- The preceding Implementation calls the helloworld method in. Net webserivce through the kSOAP package method in Android.

 

1. localhost/127.0.01 cannot be used in the ADT virtual machine. In the virtual machine, 10.0.2.2 is the local address by default. net WebService if it is localhost/webser/webtest. in the android simulator, asmx is 10.0.2.2/webser/webtest/asmx. (test the problem in person)

2. Remember to add androidmanifest. XML to allow Internet access <uses-Permission Android: Name = "android. Permission. Internet"/>

 

Turn: http://blog.csdn.net/fairyeye/article/details/6672466

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.