See "crazy android handout" page 730
Example:
Http://wenku.baidu.com/link? Url = A5SiKBUNVx-aB_hFiDuHsEN1uw_Vu4a9ODRq7G-SpiY-qW11Ve8Ba8ym9TfheAUuHJhYgdD3g05lvGNWrPBzf7JZFhUlAcZYb0MMIdrcmp3
1. Create a server:
package com.ljh.jsondemo.model;public class User {public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}private int id;private String name;private String email;private String gender; }
Package com. lstrap. jsondemo. servlet; import java. io. IOException; import java. io. printWriter; import java. util. arrayList; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. json. JSONArray; import org. json. JSONObject; import com. lstrap. jsondemo. model. user; public class JsonDemoServlet extends HttpServlet {private static final long serialVersionUID =-7379225680407826408l; private List
List;/*** process data submitted in post mode */public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );} /*** process data submitted in get mode */public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response. setContentType ("text/plain"); response. setCharacterEncoding ("UTF-8"); PrintWriter out = response. getWriter (); // prepare User data prepareData (); // JSON array JSONArray array = new JSONArray (); for (User bean: list) {// JSON object JSONObject obj = new JSONObject (); try {obj. put ("id", bean. getId (); obj. put ("name", bean. getName (); obj. put ("email", bean. getEmail (); obj. put ("gender", bean. getGender ();} catch (Exception e) {} array. put (obj);} // output out. write (array. toString (); out. flush (); out. close ();} private void prepareData () {list = new ArrayList
(); User bean1 = new User (); bean1.setId (1001); bean1.setName ("Tony"); bean1.setEmail (""); bean1.setGender ("male"); list. add (bean1); User bean2 = new User (); bean2.setId (1002); bean2.setName ("Jack"); bean2.setEmail (""); bean2.setGender ("male"); list. add (bean2); User bean3 = new User (); bean3.setId (1003); bean3.setName ("Marry"); bean3.setEmail (""); bean3.setGender ("female"); list. add (bean3); User bean4 = new User (); bean4.setId (1004); bean4.setName ("Linda"); bean4.setEmail (""); bean4.setGender ("female"); list. add (bean4 );}}
2. Create an android Client
Note that you must add the internet access permission.
Package com. lstrap. jsondemoclient; import java. io. bufferedReader; import java. io. inputStreamReader; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. client. httpClient; import org. apache. http. client. methods. httpGet; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. params. httpConnectionParams; import org. apache. http. params. httpParams; impor T org. json. JSONArray; import org. json. JSONObject; import android. OS. bundle; import android. app. activity; import android. widget. textView; public class MainActivity extends Activity {private String getContent (String url) throws Exception {StringBuilder sb = new StringBuilder (); HttpClient client = new DefaultHttpClient (); HttpParams httpParams = client. getParams (); // sets the network timeout parameter HttpConnectionParams. setConnect IonTimeout (httpParams, 3000); HttpConnectionParams. setSoTimeout (httpParams, 5000); HttpResponse response = client.exe cute (new HttpGet (url); HttpEntity entity = response. getEntity (); if (entity! = Null) {System. out. println ("test !!!! "); BufferedReader reader = new BufferedReader (new InputStreamReader (entity. getContent (), "UTF-8"), 8192); String line = null; while (line = reader. readLine ())! = Null) {sb. append (line + "\ n");} reader. close ();} return sb. toString () ;}@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); try {StringBuffer sb = new StringBuffer (); // During the test, the local machine is often used as the test server, set the IP address to 10.0.2.2String url = "192.168.136.20.: 8080/JsonDemoServer/JsonDemoServlet"; String body = getContent (url); JSONArray array = new JSONArray (body ); for (int I = 0; I <array. length (); I ++) {JSONObject obj = array. getJSONObject (I); sb. append ("id :"). append (obj. getInt ("id ")). append ("\ t"); sb. append ("name :"). append (obj. getString ("name ")). append ("\ r \ n"); sb. append ("gender :"). append (obj. getString ("gender ")). append ("\ t"); sb. append ("email :"). append (obj. getString ("email ")). append ("\ r \ n"); sb. append ("---------------------- \ r \ n");} TextView textView = (TextView) findViewById (R. id. TV _json_content); textView. setText (sb. toString ();} catch (Exception e ){}}}