获取内容资料
安卓开发

android服务器端开发

1.首先搭建服务器端。

使用MyEclipse开发工具

public class MyServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {ArrayList food = new ArrayList;Gson g = new Gson;Food f1 = new Food(1,”1111″,”1111″,”/cinema_1.jpg”);Food f2 = new Food(2,”2222″,”22222″,”/cinema_2.jpg”);Food f3 = new Food(3,”3333″,”33333″,”/food_1.jpg”);Food f4 = new Food(4,”44444″,”4444″,”/food_2.jpg”);Food f5 = new Food(5,”55555″,”5555″,”/hotel_1.jpg”);food.add(f1);food.add(f2);food.add(f3);food.add(f4);food.add(f5);boolean isNextPage = false;Map map = new HashMap;map.put(“foodList”, food);map.put(“isNextPage”, isNextPage);resp.setContentType(“text/html;charset=utf-8”);PrintWriter out = resp.getWriter;out.print(g.toJson(map));out.close;}}上述代码所需的Food实体如下:

public class Food implements Serializable{private int id;private String name;private String desc;private String imgPath;public Food(int id, String name, String desc, String imgPath) {super;this.id = id;this.name = name;this.desc = desc;this.imgPath = imgPath;}public int getId {return id;}public void setId(int id) {this.id = id;}public String getName {return name;}public void setName(String name) {this.name = name;}public String getDesc {return desc;}public void setDesc(String desc) {this.desc = desc;}public String getImgPath {return imgPath;}public void setImgPath(String imgPath) {this.imgPath = imgPath;}}我们在浏览器中访问

可以获取到结果,说明服务器端是正常的。

2.服务器端搭建好了之后,我们搭建android端

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注