1. Communicate with the server
Similar to communication between browsers and servers, Android applications can also communicate with Web servers. For example, Android applications can access servlet, JSP, and images;
By specifying content-type, the web server can specify the type of returned data. For example, text/html indicates a webpage, text/XML indicates XML data, and image/JPEG indicates an image;
2. Use XML to allow the client to communicate with the server. For example, if the android client opens the dating network, the returned data is a sequence of multiple people, including the person's name, age, and other information, how to organize the data and return it to the client? XML is very convenient. 3. The code implementation server uses dom4j to organize XML data and return it to the client. The code structure is as follows:
Package Org. xiazdong. servlet; import Java. io. ioexception; import Java. util. arraylist; import Java. util. list; import javax. servlet. servletexception; import javax. servlet. annotation. webservlet; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. dom4j. document; import Org. dom4j. export enthelper; import Org. dom4j. element; import Org. xiazdong. domain. person; @ webservlet ("/listservlet") public class listservlet extends httpservlet {Private Static final long serialversionuid = 1l; protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {dopost (request, response);} protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {list <person> List = new arraylist <person> (); for (INT I = 0; I <4; I ++) {person = new person (I, "xiazdong-" + I, 20 + I); list. add (person);} document = incluenthelper. createdocument (); element persons = incluenthelper. createelement ("persons"); document. add (persons); For (INT I = 0; I <4; I ++) {persons. addelement ("person "). addattribute ("ID", list. get (I ). GETID () + ""); persons. addelement ("name "). addtext (list. get (I ). getname (); persons. addelement ("Age "). addtext (list. get (I ). getage () + "");} string xml = document. asxml (); // use dom4j to organize an XML string response. setcontenttype ("text/XML; charset = UTF-8"); // set the type of the returned value response. getoutputstream (). write (XML. getbytes ("UTF-8"); // sets the return value }}If the browser is accessed, the effect is as follows: (Note: XML declaration is always not displayed when Chrome is opened. Open it with IE.) Next, we can develop the client to display the data in the list. Add the following to androidmanifest. xml:
<uses-permission android:name="android.permission.INTERNET" />
The layout of each item is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/id" /> <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:id="@+id/name" /><TextView android:layout_width="50dp" android:layout_height="wrap_content" android:id="@+id/age" /></LinearLayout>
Mainactivity. Java
Package Org. xiazdong. personlist; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import Org. xiazdong. domain. person; import Org. xmlpull. v1.xmlpullparser; import android. app. activity; import android. OS. bundle; import android. util. log; import android. util. XML; import android. widget. Listview; import android. widget. simpleadapter; public class mainactivity extends activity {private listview; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); listview = (listview) This. findviewbyid (R. id. listview); List <person> Persons = NULL; try {persons = getxmldata ();} catch (exception e) {e. printstacktrace ();} List <Ma P <string, Object> maps = new arraylist <Map <string, Object> (); For (person: Persons) {hashmap <string, object> map = new hashmap <string, Object> (); map. put ("ID", person. GETID (); map. put ("name", person. getname (); map. put ("Age", person. getage (); maps. add (MAP);} simpleadapter adapter = new simpleadapter (this, maps, R. layout. item, new string [] {"ID", "name", "Age"}, new int [] {R. id. ID, R. id. name, R. id. age}); listview. setadapter (adapter);} // read XML data and convert it to list <person> private list <person> getxmldata () throws exception {list <person> Persons = new arraylist <person> (); person = NULL; Url url = new URL ("http: // 192.168.0.103: 8080/Server/listservlet "); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmethod ("get"); If (Conn. getresponsecode () = 200) {inputstream in = Conn. getinputstream (); xmlpullparser parser = xml. newpullparser (); parser. setinput (in, "UTF-8"); int event = parser. geteventtype (); While (event! = Xmlpullparser. end_document) {log. I ("start_document", "start_document"); Switch (event) {Case xmlpullparser. start_tag: If ("person ". equals (parser. getname () {person = new person (); person. setid (integer. parseint (parser. getattributevalue (0);} If ("name "). equals (parser. getname () {person. setname (parser. nexttext ();} If ("age ". equals (parser. getname () {person. setage (integer. parseint (parser. nexttext ();} break; Case xmlpullparser. end_tag: If ("person ". equals (parser. getname () {persons. add (person); log. I ("person", person. tostring ();} break;} event = parser. next () ;}} return persons ;}}
The effect is as follows: