发送数组列表由servlet来Android应用程序数组、应用程序、列表、servlet

2023-09-06 13:49:47 作者:你是逗逼派来的猴子么

我如何发送一个数组列表,从一个servlet到一个Android应用程序?

How can I send a array list from a servlet to an Android application?

推荐答案

要的一点,你需要将其转换为字符串在一些标准格式,那么它可以是解析并用了现有的库构建的。例如,JSON,XML或CSV,所有这一切都是标准化的,可更换的字符串格式中存在的很多编程语言的很多分析/建筑库。

To the point, you need to convert it to a String in some standard format which can then be parsed and built using the existing libraries. For example, JSON, XML or CSV, all which are standardized and exchangeable string formats for which a lot of parsing/building libraries exist in a lot of programming languages.

Android有在的 org.json 包。它甚至在的 org.xml.sax中包。我不知道是否有一个内置的CSV库,但似乎没有了。 Java EE的在 JAXB 一个伟大的内置XML解析器的味道,但它不具备一个内置JSON解析器。只有JAX-RS实现(泽西岛,RestEasy的,等等)提供了一个JSON解析器。如果你可以改变你的servlet是一个JAX-RS web服务,而不是,你就会有能够XML和JSON返回非常最小的努力的好处。另请参见的Servlet VS REST风格。

Android has a builtin JSON parser in the org.json package. It has even a builtin XML parser in the org.xml.sax package. I'm not sure if there's a builtin CSV library, but there appears to be none. Java EE has in flavor of JAXB a great builtin XML parser, but it doesn't have a builtin JSON parser. Only the JAX-RS implementations (Jersey, RESTeasy, etc) provide a JSON parser. If you can change your servlet to be a JAX-RS webservice instead, you'll have the benefit of being able to return both XML and JSON with very minimal effort. See also Servlet vs RESTful.

哪种格式的选择依赖于唯一的功能要求。例如,被小服务程序应该被重用于其他服务吗?什么是目标客户?等等。

Which format to choose depends on the sole functional requirements. For example, is the servlet supposed to be reused for other services? What are the target clients? Etcetera.

在任何方式,JSON格式通常是更简洁,最近比XML更受欢迎,所以我只给它以JSON格式将数据传输开球的例子,我会假设你真的想这样做与一个普通的servlet的,而不是JAX-RS。在这个例子中,你只需要下载和删除一个JSON库,如 GSON 在 / WEB-INF / lib目录为了转换Java对象到JSON在Servlet。

In any way, the JSON format is usually more terse and lately more popular than XML, so I'll just give a kickoff example which transfers the data in the JSON format and I'll assume that you really want to do it with a plain vanilla servlet instead of JAX-RS. For this example, you only need to download and drop a JSON library such as Gson in /WEB-INF/lib in order to convert Java objects to JSON in the servlet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<String> list = new ArrayList<String>();
    list.add("item1");
    list.add("item2");
    list.add("item3");
    String json = new Gson().toJson(list);

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
}

这将建立并返回列表下面的JSON格式:

This will build and return the List in the following JSON format:

["item1","item2","item3"]

这是反过来可分析由 org.json 在Android的API如下:

This is in turn parseable by org.json API in Android as follows:

String jsonString = getServletResponseAsStringSomehow(); // HttpClient?
JSONArray jsonArray = new JSONArray(jsonString);
List<String> list = new ArrayList<String>();

for (int i = 0; i < jsonArray.length(); i++) {
    list.add(jsonArray.getString(i));
}

// Now you can use `list`.