Recent company leaders to do the app, although they used to do the app test, but a lot of things are not very clear, here to record their history of this period of life.
1, build the server side, before doing the test, they use is apache+mysql+php, and their school when contact with some Java knowledge, use Java+servlet+jsp+sqlserver+tomcat to do.
2, because they have not written code for a long time, all writing is a comparative basis of the example. The following is an example of the Android parsing JSON data based on the Baidu Library.
First server side:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appID= "webapp_id"version= "2.4"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">3 <Display-name>Servlettest</Display-name>4 <welcome-file-list>5 <Welcome-file>Index.html</Welcome-file>6 <Welcome-file>Index.htm</Welcome-file>7 <Welcome-file>index.jsp</Welcome-file>8 <Welcome-file>Default.html</Welcome-file>9 <Welcome-file>Default.htm</Welcome-file>Ten <Welcome-file>default.jsp</Welcome-file> One </welcome-file-list> A - - <servlet> the <Servlet-name>Test</Servlet-name> - <Servlet-class>Com.android.servlet.ServletDemo</Servlet-class> - </servlet> - <servlet-mapping> + <Servlet-name>Test</Servlet-name> - <Url-pattern>/test</Url-pattern> + </servlet-mapping> A </Web-app>
Servletdemo.class
1 PackageCom.android.servlet;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 Importjava.util.ArrayList;6 Importjava.util.List;7 8 Importjavax.servlet.ServletException;9 ImportJavax.servlet.http.HttpServlet;Ten Importjavax.servlet.http.HttpServletRequest; One ImportJavax.servlet.http.HttpServletResponse; A - ImportNet.sf.json.JSONArray; - ImportNet.sf.json.JSONObject; the - Public classServletdemoextendsHttpServlet { - - PrivateList<user>list; + @Override - protected voiddoget (httpservletrequest req, HttpServletResponse resp) + throwsservletexception, IOException { A //TODO auto-generated Method Stub at - -Resp.setcontenttype ("Text/plain"); -Resp.setcharacterencoding ("Utf-8"); -PrintWriter out=Resp.getwriter (); - inlist=Praparedata (); - //Out.write (list.tostring ()); + A theJsonarray array=NewJsonarray (); + - for(User bean:list) $ { $Jsonobject obj=NewJsonobject (); - - Try{ the - WuyiObj.put ("id", Bean.getid ()); theObj.put ("name", Bean.getname ()); -Obj.put ("Email", Bean.getemail ()); WuObj.put ("Gender", Bean.getgender ()); - About $ } - Catch(Exception e) { - - } A + the array.add (obj); - $ the } the Out.write (array.tostring ()); the //out.write ("Nihao!!!!"); the Out.flush (); - out.close (); in the the } About the @Override the protected voidDoPost (httpservletrequest req, HttpServletResponse resp) the throwsservletexception, IOException { + //TODO auto-generated Method Stub - Super. Doget (req, resp); the }Bayi PrivateList<user>Praparedata () the { thelist=NewArraylist<user>(); - -User user1=NewUser (); theUser1.setid (1001); theUser1.setname ("What a Thing"); theUser1.setemail ("[Email protected]"); theUser1.setgender ("Male"); - List.add (user1); the the returnlist; the 94 } the the}
User
1 PackageCom.android.servlet;2 3 Public classUser {4 Private intID;5 PrivateString name;6 PrivateString Email;7 PrivateString gender;8 Public intgetId () {9 returnID;Ten } One Public voidSetId (intID) { A This. ID =ID; - } - PublicString GetName () { the returnname; - } - Public voidsetName (String name) { - This. Name =name; + } - PublicString Getemail () { + returnemail; A } at Public voidsetemail (String email) { - This. email =email; - } - PublicString Getgender () { - returngender; - } in Public voidSetgender (String gender) { - This. Gender =gender; to } + - the}
Enter the address in the browser: http://localhost:8080/ServletTest/test (note, if it is another computer on the LAN, change the localhost to the IP address of the one you want to access.)
[{"id": 1001, "name": "What Thing", "email": "[email protected]", "gender": "Male"}]
Not to be continued
Step by step, slowly will be better, believe in yourself!
App Development History---1,servlet return JSON as an instance of the Android interface