Document directory
- Core server code:
- Core client code:
We completed the "android entry: communicating with the server through XML data" function, but data transmission uses JSON instead of XML;
Note:
(1) When JSON is returned, the Content-Type is text/JSON;
(2) Parse JSON data and generate JSON data through a JSON package;
(3) JSON packages have been integrated in Android;
I. Core code introduction:
(1) jsonstringer Stringer = new jsonstringer ();
(2) Stringer. Array (). endarray ();
(3) Stringer. Object (). Key ("key"). Value ("value"). endobject ();
(4) string STR = Stringer. tostring ();
Core client code:
(1) jsonarray array = new jsonarray (string Str );
// Convert the string to the jsonarray format
(2) jsonobject object = array. getjsonobject (INT index );
(3) int value = object. getint (string name );
(4) string value = object. getstring (string name );
Ii. All code servers:
Listservlet. Java
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. JSON. jsonstringer; 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) ;}// organize list <person> into a JSON string jsonstringer Stringer = new jsonstringer (); try {Stringer. array (); For (person: List) {Stringer. object (). key ("ID "). value (person. GETID ()). key ("name "). value (person. getname ()). key ("Age "). value (person. getage ()). endobject ();} Stringer. endarray ();} catch (exception e) {} response. getoutputstream (). write (Stringer. tostring (). getbytes ("UTF-8"); response. setcontenttype ("text/JSON; charset = UTF-8"); // The JSON type is text/JSON }}
The following figure shows the Access Effect in the browser:
Client:
Mainactivity. Java
Package Org. xiazdong. personlist; import Java. io. bytearrayoutputstream; import Java. io. ioexception; 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. JSON. jsonarray; import Org. JSON. jsonobject; 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 = getjsondata ();} catch (exception e) {e. printstacktrace () ;}list <Map <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 ADA Pter = 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 JSON data and convert it to list <person> private list <person> getjsondata () throws exception {list <person> Persons = new arraylist <person> (); Url url = new URL ("http: // 192.168.0.103: 8080/Server/listservlet "); httpurlconnection conn = (httpurlconnection) Ur L. openconnection (); Conn. setrequestmethod ("get"); If (Conn. getresponsecode () = 200) {inputstream in = Conn. getinputstream (); Persons = parsejson (in);} return persons;} private list <person> parsejson (inputstream in) throws exception {list <person> Persons = new arraylist <person> (); person = NULL; string STR = read (in); jsonarray array = new jsonarray (STR ); int length = array. length (); For (INT I = 0; I <Leng Th; I ++) {jsonobject object = array. getjsonobject (I); person = new person (object. getint ("ID"), object. getstring ("name"), object. getint ("Age"); persons. add (person);} return persons;} private string read (inputstream in) throws ioexception {byte [] data; bytearrayoutputstream bout = new bytearrayoutputstream (); byte [] Buf = new byte [1024]; int Len = 0; while (LEN = in. read (BUF ))! =-1) {bout. Write (BUF, 0, Len);} DATA = bout. tobytearray (); return new string (data, "UTF-8 ");}}
Summary:
JSON and XML are both data transmission formats;