Data is returned to the Information client in JSON format, which is more efficient than parsing and transmitting xml files.

Source: Internet
Author: User

1. in android, the performance of data transmission in xml format is very low. 2. data transmission using json is better than data transmission using xml files ------------------------------------------------------------------- 3. requirement: if a user adds a suffix to the path, json data is returned and sent to the android client. If no suffix is added, xml data is returned, the original path for the android client is http: // 192.168.1.110: 6118/VideoNews/ListServlet with the Suffix: http: // 127.0.0.1: 6118/VideoNews/ListServlet? Format = json ----------------------------------------------------------------------------- 4. the following figure shows all source code for data transmission using json.. the server returns json data in the VideoNewsTest web Project:/VideoNewsTest/src/com/credram/servlet/ListServlet. javapackage com. credram. servlet; import java. io. IOException; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServlet Request; import javax. servlet. http. httpServletResponse; import com. credream. entity. news; import com. credream. service. videoNewsService; import com. credream. service. impl. videoNewsServiceImpl;/*** Servlet implementation class ListServlet */public class ListServlet extends HttpServlet {private static final long serialVersionUID = 1L; private VideoNewsService service = new VideoNewsServiceImpl (); public L IstServlet () {}/*** @ see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) */protected void doPost (HttpServletRequest request, HttpServletRe Response se response) throws ServletException, IOException {List <News> videos = service. getLastNews (); String format = request. getParameter ("format"); if ("json ". equals (format) {// json: the data format []. In js syntax, [] indicates the array // constructs the object in []: // [{id: 56, title: "xiaofeng", timelength: 90}, {id: 36, title: "xiaofeng", timelength: 90}] StringBuilder builder = new StringBuilder (); builder. append ("["); for (News news: videos) {builder. append ("{"); builder. ap Pend ("id :"). append (news. getId ()). append (","); builder. append ("title :\""). append (news. getTitle ()). append ("\", "); builder. append ("timelength :"). append (news. getTimelength (); builder. append ("},");} builder. deleteCharAt (builder. length ()-1); builder. append ("]"); request. setAttribute ("json", builder. toString (); request. getRequestDispatcher ("/WEB-INF/page/jsonvideosnews. jsp "). forward (request, response);} els E {request. setAttribute ("videos", videos); request. getRequestDispatcher ("/WEB-INF/page/videosnews. jsp "). forward (request, response) ;}} role: when a user accesses a URL with parameters: http: // 127.0.0.1: 6118/VideoNews/ListServlet? Format = json: json data is returned. xml data is returned when the user access address is http: // 192.168.1.110: 6118/VideoNews/ListServlet --------------------------------------------------- B. in the android project:/GetNetNews/src/com/credream/service/VideoNewsService. how to parse json data added to the java file:/*** get the parsed json data, returns list * @ return * @ throws Exception */public static List <News> getJSONLastNews () throws Exception {String path = "http: // 192.168.1.110: 6118/VideoNews/ListServ Let? Format = json "; URL url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setConnectTimeout (5000); conn. setRequestMethod ("GET"); if (conn. getResponseCode () = 200) {InputStream inStream = conn. getInputStream (); return parseJSON (inStream);} return null;} ------------------------------------------------------------- c. A previously written tool class is used here to copy/netimage/src/com/credream/util/StreamTool of the incoming data stream in the form of byte. java, to the GetNetNews Project (this project) -------------------------------------------- d. json Parsing Method:/*** parse json data * @ param inStream * @ return */private static List <News> parseJSON (InputStream inStream) throws Exception {List <News> newes = new ArrayList <News> (); byte [] data = StreamTool. read (inStream); // The String returned by the server, which is text data String json = new String (data); // converts data, string // converts a json string to an array object JSONArray array = new JSONArray (json); for (int I = 0; I <array. length (); I ++) {// [{id: 56, title: "xiaofeng", timelength: 90}, {id: 36, title: "xiaofeng ", timelength: 90}] // A braces is a json object JSONObject jsonObject = array. getJSONObject (I); // obtain the I-th json; int id = jsonObject. getInt ("id"); String title = jsonObject. getString ("title"); int timelength = jsonObject. getInt ("timelength"); News news = new News (id, title, timelength); newes. add (news);} return newes ;}-------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.