In Android applications, we often interact and communicate with applications on the server (such as J2EE or. NET) on the network.
This example describes how to call the WebService provided by the server in Android to implement a typical distributed application.
Package CN. itcast. mobile. address; import Java. io. inputstream; import CN. itcast. service. mobileinfoservice; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. widget. button; import android. widget. edittext; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {private edittext mobiletext; private textview addressview; Private Static final string tag = "mainactivity"; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mobiletext = (edittext) This. findviewbyid (R. id. mobile); addressview = (textview) This. findviewbyid (R. id. address); button = (button) This. findviewbyid (R. id. button); button. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {string mobile = mobiletext. gettext (). tostring (); inputstream instream = This. getclass (). getclassloader (). getresourceasstream ("mobilesoap. XML "); try {addressview. settext (mobileinfoservice. getmobileaddress (instream, mobile);} catch (exception e) {log. E (TAG, E. tostring (); toast. maketext (mainactivity. this, "query failed", 1 ). show ();}}});}}
Interface file:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/mobile" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/mobile" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/address" /></LinearLayout>
Key implementation code:
Package CN. itcast. service; import Java. io. inputstream; import Java. io. outputstream; import java.net. httpurlconnection; import java.net. URL; import Java. util. hashmap; import Java. util. map; import Java. util. regEx. matcher; import Java. util. regEx. pattern; import Org. xmlpull. v1.xmlpullparser; import android. util. XML; import CN. itcast. utils. streamtool; public class mobileinfoservice {Private Static string readsoapfi Le (inputstream instream, string mobile) throws exception {byte [] DATA = streamtool. readinputstream (instream); string soapxml = new string (data); Map <string, string> Params = new hashmap <string, string> (); Params. put ("mobile", mobile); Return Replace (soapxml, Params);} public static string Replace (string XML, Map <string, string> Params) throws exception {string result = xml; If (Params! = NULL &&! Params. isempty () {for (map. entry <string, string> entry: Params. entryset () {string name = "\\{ 1} quot; + entry. getkey (); pattern = pattern. compile (name); matcher = pattern. matcher (result); If (matcher. find () {result = matcher. replaceall (entry. getvalue () ;}}return result;} public static string getmobileaddress (inputstream instream, string mobile) throws exception {string soap = readsoapfile (I Nstream, mobile); byte [] DATA = soap. getbytes (); Url url = new URL ("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("Post"); Conn. setconnecttimeout (5*1000); Conn. setdooutput (true); // if you submit data through post, you must set to allow external output data Conn. setrequestproperty ("Content-Type", "application/soap + XML; charset = UTF-8"); Conn. setreque Stproperty ("Content-Length", String. valueof (data. length); outputstream outstream = Conn. getoutputstream (); outstream. write (data); outstream. flush (); outstream. close (); If (Conn. getresponsecode () = 200) {return parseresponsexml (Conn. getinputstream ();} return NULL;} Private Static string parseresponsexml (inputstream instream) throws exception {xmlpullparser parser = xml. newpullparser (); parser. setinput (instre Am, "UTF-8"); int eventtype = parser. geteventtype (); // generates the first event while (eventtype! = Xmlpullparser. end_document) {// as long as it is not a document end event switch (eventtype) {Case xmlpullparser. start_tag: string name = parser. getname (); // get the name of the element currently pointed to by the parser if ("getmobilecodeinforesult ". equals (name) {return parser. nexttext ();} break;} eventtype = parser. next () ;}return null ;}}
Finally, I would like to remind you to add the network access permission to the project list file.
<! -- Access Network permissions -->
<Uses-Permission Android: Name = "android. Permission. Internet"/>