Explain how the Java server provides the interface to the mobile terminal, in what data format to provide, the mobile side is how to request the server, receive and parse the return data.
Service side: Or in the original S2SH framework of the project (build ssh detailed steps and related instructions), to join the servlet to provide interface to the terminal things.
Android: Use a network access framework okhttp to request data from the server.
Service side:
The servlet receives a GET, POST request from the mobile side and writes the data to be returned in JSON format after the corresponding logical processing.
Operations on the database the traditional servlet is JDBC, but the operation is too cumbersome to reuse hibernate in the project. So how do I use hibernate in a servlet?
The servlet is defined in Web. XML, with the new Classpathxmlapplicationcontext ("Applicationcontext.xml") in the Servlet class, and the Spring container is available, Hibernate can then be taken to manipulate the database.
ApplicationContext ac=new classpathxmlapplicationcontext ("Applicationcontext.xml"); Userdao userdao= (Userdao) Ac.getbean ("Userdao"); List<user> Userlist=userdao.findall ();
When using JSON, you must import the jar:
Commons-beanutils-1.7.0.jar
Commons-collections-3.2.1.jar
Commons-httpclient-3.1.jar
Commons-lang-2.3.jar
Commons-logging-1.1.1.jar
Ezmorph-1.0.3.jar
Json-lib-2.2.3-jdk15.jar
Userservlet.java
Package Joanna.yan.servlet;import Java.io.ioexception;import Java.io.printwriter;import java.util.List;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Joanna.yan.dao.userdao;import Joanna.yan.entity.user;import Net.sf.json.jsonarray;import Net.sf.json.JSONObject; Import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Userservlet extends HttpServlet{@ Override protected void doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOE xception {doPost (req, resp); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexc Eption, IOException {System.out.println ("was visited! "); PrintWriter Out=resp.getwriter (); Jsonobject json=new jsonobject (); JsOnobject json1=new jsonobject (); String msg= "Success"; int errorcode=0; ApplicationContext ac=new classpathxmlapplicationcontext ("Applicationcontext.xml"); try {Userdao userdao= (Userdao) Ac.getbean ("Userdao"); List<user> Userlist=userdao.findall (); if (Userlist.size () >0) {Jsonarray jsonarray=new jsonarray (); for (User user:userlist) {json1.put ("id", User.getid ()); Json1.put ("username", user.getusername ()); Json1.put ("Age", User.getusername ()); Jsonarray.add (Json1); } json.put ("Data", Jsonarray); }else{msg= "No data to meet the requirements"; errorcode=1; Json.put ("Data", ""); }} catch (Exception e) {e.printstacktrace (); msg = "System error"; ErrorCode = 10000; }finally{json.put ("MessagE ", msg); Json.put ("ErrorCode", errorcode); Out.print (JSON); Out.flush (); Out.close (); } }}
Configuring Userservlet in Web. xml
<servlet> <servlet-name>userServlet</servlet-name> <servlet-class> joanna.yan.servlet.userservlet</servlet-class> </servlet> <servlet-mapping> < servlet-name>userservlet</servlet-name> <url-pattern>/user</url-pattern> </ Servlet-mapping>
Android side:
Using okhttp, download Okhttp-2.7.0.jar and Okio-1.6.0.jar into Libs under the official website and add Build Path. Add Gson-2.2.4.jar to parse JSON data at the same time
Mainactivity.java
Package Com.example.commussh;import Java.io.ioexception;import Java.util.map;import android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.TextView;import Com.google.gson.gson;import Com.squareup.okhttp.okhttpclient;import Com.squareup.okhttp.request;import Com.squareup.okhttp.response;public class Mainactivity extends Activity {TextView tv_show; Button Btn_search; Okhttpclient client; Gson Gson; Static class Gist {map<string, gistfile> files; } static class Gistfile {String content; } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); client= new Okhttpclient (); Gson=new Gson (); tv_show= (TextView) Findviewbyid (r.id.tv_show); Btn_search= (Button) Findviewbyid (R.id.btn_search); Btn_search.setonclicklistener (New View.onclicklistEner () {@Override public void OnClick (View v) {New Thread (new Runnable ( {@Override public void run () {Request Request = new Request.builder (). URL ("Http://localhost:8080/SSH/user"). Bui LD (); Response Response; try {response = Client.newcall (Request). Execute (); if (!response.issuccessful ()) throw new IOException ("Unexpected code:" + response); Gist Gist = Gson.fromjson (Response.body (). Charstream (), gist.class); For (map.entry<string, gistfile> entry:gist.files.entrySet ()) {System.out.println (en) Try.getkey ()); System.out.println (Entry.getvalue (). content); } } catch (IOException e) {e.printstacktrace (); }}). Start (); } }); }}
Test results:
Source: Http://pan.baidu.com/s/1mgU7xKs Extract Code: pe9i
Java server provides external interface and Android server requests data