简体   繁体   English

java服务器向android客户端发送字符串数组列表

[英]java server send list of array of string to android client

i want to send request from android to java tomcat server using servlet, then the server will send huge data to client, the data contains of many rows, every row has three parameters: 我想用android向java tomcat服务器发送请求,然后服务器会向客户端发送大量数据,数据包含很多行,每行有三个参数:

competitionID, marks, numberOfQuestions

code to send request from android to server 用于将请求从android发送到服务器的代码

HttpClient client = new DefaultHttpClient();
        URI website;
            website = new URI("http://" + HostIP
                    + ":8080/LocalizedBasedComptitionServer/JoinCompetition");
            HttpPost request = new HttpPost();
            request.setHeader("ID", ID + "");
            request.setURI(website);
            HttpResponse response = client.execute(request);

servlet code servlet代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletOutputStream out = response.getOutputStream();
        CellDatabase cDB = new CellDatabase();
                List<String[]>list = new LinkedList<String[3]>();
        out.flush();
    }

how can i send the List list and how can i receive it? 我如何发送列表list ,我该如何收到它?

There are a few ways to achieve this: 有几种方法可以实现这一目标:

1.Format the response as a JSON array, eg: 1.将响应格式化为JSON数组,例如:

[ 
   { competitionId : "6", marks : "2", numberOfQuestions : "3"},
   { competitionId : "2", marks : "1", numberOfQuestions : "5"},
]

Then your client will just parse this JSON string into array 然后您的客户端将只将此JSON字符串解析为数组

2.Create your own delimited string result, eg: 6,2,3|2,1,5 2.创建自己的分隔字符串结果,例如:6,2,3 | 2,1,5

Since you know the response will be an array of 3 elements, you can first separate items by a special character '|', then for each array item split it again by delimiter ',' and you will get your competition id, marks, and num questions. 既然您知道响应将是一个包含3个元素的数组,您可以先用特殊字符“|”分隔项目,然后对每个数组项目再用分隔符“,”分隔它,您将得到您的竞争ID,标记和num问题。

This method is quick and easy but difficult to maintain: If you add/remove new elements, or change delimiter on the server side, your client will break. 此方法快速简便但难以维护:如果在服务器端添加/删除新元素或更改分隔符,则客户端将中断。 So using the JSON method probably is the way to go. 所以使用JSON方法可能就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM