Recently plan to make a hotel wireless ordering app, you need to put the picture on the server side of the folder, the client through the Update menu button to get the latest food information. To get the information of the picture first need to get the name of the picture, the corresponding menu price and the path of the picture, so you need to search the server side of all the pictures under the folder and package the data into JSON format forwarded to the client, in the format [{name=], price= price, path= " File path ", category=" dishes Category: such as Sichuan, Hunan and other "}". The server side is deployed in Tomcat using SERVLET+JSP, and the code that gets all the picture information in the file directory and encapsulates it in JSON format is given below. The directory where the server-side picture is stored is shown below.
Packagecom.restaurant.utils;ImportJava.io.File; Public classJsonutils { Public Staticstring Getjson (String strpath) {StringBuilder builder=NewStringBuilder (); Builder.append (‘[‘); File File=NewFile (strpath); File[] FileList=File.listfiles (); for(inti = 0; i < filelist.length; i++) { if(Filelist[i].isdirectory ()) {File Mfile=NewFile (Filelist[i].getpath ()); File[] Mfilelist=Mfile.listfiles (); for(intj = 0; J < Mfilelist.length; J + +) {String name= ""; String Price= ""; String Path= ""; String category= ""; String Str=Mfilelist[j].getname (); String[] Strlist= Str.split ("\\$"); Name= Strlist[0]; Strlist= Strlist[1].split ("\ \.")); Price= Strlist[0]; Path=Mfilelist[j].getpath (); Strlist=path.split ("\\\\"); Path=""; for(String mstr:strlist) {path=path+mstr+ "\\\\"; } Path=path.substring (0, Path.length ()-2); Category=mfilelist[j].getparent (). substring (Mfilelist[j].getparent (). LastIndexOf ("\ \") + 1); Builder.append ("{name:\"). Append (name). Append ("\", "); Builder.append ("Price:"). Append, append (', '); Builder.append ("Path:\"). Append (Path). Append ("\", "); Builder.append ("Category:\" "). Append (category). Append ("\"},"); }}} Builder.deletecharat (Builder.length ()-1); Builder.append (‘]‘); returnbuilder.tostring (); }}
Create a servlet file to invoke the above Getjson () method in its doget () method, and move the client request to a JSP page to get the JSON string in the JSP through an El expression.
PackageCom.restaurant.servlet;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importcom.restaurant.utils.*;/*** Servlet Implementation class Updatepictureservlet*/@WebServlet ("/updatepictureservlet") Public classUpdatepictureservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; Private StaticString strpath = "E:\\webtest\\restaurant\\webcontent\\pictures"; /** * @seeHttpservlet#doget (httpservletrequest request, HttpServletResponse * response)*/ protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method StubRequest.setattribute ("JSON", Jsonutils.getjson (strpath)); Request.getrequestdispatcher ("/web-inf/page/jsondata.jsp"). Forward (request, response); } /** * @seeHttpservlet#dopost (httpservletrequest request, HttpServletResponse * response)*/ protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stub }}
JSP page content is as follows:
<%@ page language= "java" contenttype= "Text/plain; Charset=utf-8 "pageencoding=" UTF-8 "%>${json}
Deploy the project to the server and enter it in the browser: Http://localhost:8080/Restaurant/UpdatePictureServlet after carriage return, the effect is as follows:
The Android client obtains JSON data through the HTTP protocol, first encapsulating a Picturebean object, which is used to receive the parsed JSON format, while creating a database table corresponding to the object, storing the object list directly in the SQLite database for easy local reading. here is the program code that gets the JSON data from the server and stores it in the Picturebean object:
PackageCom.example.service;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.json.JSONArray;ImportOrg.json.JSONObject;ImportAndroid.util.Log;ImportCom.example.domain.PictureBean;ImportCom.example.utils.StreamTool; Public classUptadepictureservice { Public StaticList<picturebean> Getjsonpictures ()throwsException {String path= "Http://192.168.1.103:8080/Restaurant/UpdatePictureServlet"; URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setconnecttimeout (5000); Conn.setrequestmethod ("GET"); if(Conn.getresponsecode () = = 200) {InputStream instream=Conn.getinputstream (); returnParsejson (instream); } return NULL; } /*** Parse JSON data * *@paraminstream *@return */ Private StaticList<picturebean>Parsejson (InputStream instream)throwsException {List<PictureBean> Pictures =NewArraylist<picturebean>(); byte[] data =Streamtool.read (instream); String JSON=NewString (data); LOG.D ("Mainactivity", JSON); Jsonarray Array=NewJsonarray (JSON); for(inti = 0; I < Array.Length (); i++) {Jsonobject jsonobject=Array.getjsonobject (i); Picturebean Picture=NewPicturebean (jsonobject.getstring ("name"), Jsonobject.getint ("Price"), Jsonobject.getstring ("path"), jsonobject.getstring ("Category")); Pictures.add (picture); } returnPictures; }}
The above is the picture data to be packaged in JSON format, as well as the Android client to obtain image information and save as a Picturebean object of the entire process, the database of the code is relatively simple, not posted here. The download of the image and the content of the interface Display section will be described in the next blog post.
The server returns data in JSON format to the Android client