简体   繁体   中英

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:

competitionID, marks, numberOfQuestions

code to send request from android to server

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

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?

There are a few ways to achieve this:

1.Format the response as a JSON array, eg:

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

Then your client will just parse this JSON string into array

2.Create your own delimited string result, eg: 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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