[英]java server send list of array of string to android client
我想用android向java tomcat服務器發送請求,然后服務器會向客戶端發送大量數據,數據包含很多行,每行有三個參數:
competitionID, marks, numberOfQuestions
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);
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();
}
我如何發送列表list
,我該如何收到它?
有幾種方法可以實現這一目標:
1.將響應格式化為JSON數組,例如:
[
{ competitionId : "6", marks : "2", numberOfQuestions : "3"},
{ competitionId : "2", marks : "1", numberOfQuestions : "5"},
]
然后您的客戶端將只將此JSON字符串解析為數組
2.創建自己的分隔字符串結果,例如:6,2,3 | 2,1,5
既然您知道響應將是一個包含3個元素的數組,您可以先用特殊字符“|”分隔項目,然后對每個數組項目再用分隔符“,”分隔它,您將得到您的競爭ID,標記和num問題。
此方法快速簡便但難以維護:如果在服務器端添加/刪除新元素或更改分隔符,則客戶端將中斷。 所以使用JSON方法可能就是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.