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 the user is on the road... 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 to the android client is: http://192.168.1.110:6118/VideoNews/ListServlet The suffix path is: 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. httpServletRequest; 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 ListServlet () {}/*** @ see HttpServlet # doGet (HttpServletRequest request, response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, request, response);}/*** @ see HttpServlet # doPost (HttpServletRequest request, response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {List 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. append ("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);} else {request. setAttribute ("videos", videos); request. getRequestDispatcher ("/WEB-INF/page/videosnews. jsp "). forward (request, response) ;}}----------------------------------------------------------- role of this servlet: when a user accesses a URL with parameters: http://127.0.0.1:6118/VideoNews/ListServlet?format=json Json data will be returned when the user access address: http://192.168.1.110:6118/VideoNews/ListServlet The returned xml data is ----------------------------------------------------- B. in the android project:/GetNetNews/src/com/credream/service/VideoNewsService. add a method to parse json data to the java file:/*** get the parsed json data and return list * @ return * @ throws Exception */public static List GetJSONLastNews () throws Exception {String path = "http: // 192.168.1.110: 6118/VideoNews/ListServlet? 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 ParseJSON (InputStream inStream) throws Exception {List Newes = new ArrayList (); 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 // [{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.