I. Building the environment in the Java EE project
1. Importing related JAR Packages
2. Build the relevant packages and classes
3. Simple implementation of the methods in the class
User:
Declares the following properties, implements the parameterless constructor and the parameter constructor, and the respective get and set methods
Userdao:
Userdaoimpl:
Jsonservlet:
<span style= "White-space:pre" ></span>protected void doget (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//from database Userdao Daoimpl = new Userdaoimpl (); list<user> entities = Daoimpl.findall ();//Convert the list collection to a JSON data format Jsonarray Jsonarray = Jsonarray.fromobject ( entities); Response.setcontenttype ("Application/json");//Data Response.getwriter () of the response type JSON. Write ( Jsonarray.tostring ()); Response.getwriter (). Close (); protected void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception, IOException { Doget (request, response);}
Second, the processing of the server-side return JSON data in the Android project
1. Importing the JAR Package
2. Add Permissions
3. Implementing the Layout file
<!--V-level in MVC mode-- <listview android:id= "@+id/lv_users" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_alignparentbottom= "true"/>
The main contents of 4.mainactivity.java are shown in the following detail code:
Package Www.csdn.net.activity;import Java.util.arraylist;import Java.util.list;import org.apache.http.Header; Import Org.json.jsonarray;import org.json.jsonexception;import org.json.jsonobject;import android.app.Activity; Import Android.os.bundle;import Android.widget.arrayadapter;import Android.widget.listview;import Android.widget.toast;import Com.loopj.android.http.asynchttpclient;import Com.loopj.android.http.jsonhttpresponsehandler;public class Mainactivity extends Activity {private ListView lv_users; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); lv_users = (ListView) Findviewbyid (r.id.lv_users); InitData ()//m layer//initdata itself is processed in a sub-thread} private void InitData () {//Access server-side get JSON data//Create client Object asynchttpclient = new Asynchttpclient (); String url = "Http://172.16.237.227:8080/video/JsonServlet"; Toast.maketext (this, "Send request to Server", Toast.length_long). Show (); Client.get (URL, new Jsonhttpresponsehandler () {// Return Jsonarray Object | Jsonobject object @overridepublic void onsuccess (int statusCode, header[] headers,jsonarray response) {super.onsuccess ( StatusCode, headers, response); if (StatusCode = = 200) {//Storage array variable list<string> objects = new arraylist<> (); for ( int i = 0; I < response.length (); i++) {try {//Get a specific Jsonobject object Jsonobject obj = Response.getjsonobject (i); Objects.add (obj.getstring ("name"));} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} C control layer is mainly for data processing arrayadapter<string> adapter = new Arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_1, objects);//Set the displayed content Lv_users.setadapter (adapter);//C Vacant}});}}
5. The implementation effect is as follows:
Third, supplementary
1. Jsonobject objects can get property values for a specified property name by getting ("property name"), GetString ("property name"), GetInt ("property name"), and so on.
For example: In this case, you can pass System.out.println ("ordinal:" + obj.getint ("id") + "Name:" + obj.getstring ("name") + "---Password:" + obj.getstring ("Pass ") +"---"+ obj.get (" RegDate ") to verify the results.
2. The cause of the bug may be:
Internet permissions are not added, the server does not start, access to the server path is wrong, the control object is not acquired, the settings displayed should be the Lv_users.setadapter () method instead of the SetText () method, and so on.