首页 > 代码库 > app开发历程---1,servlet 返回JSON作为android 接口实例

app开发历程---1,servlet 返回JSON作为android 接口实例

最近公司领导要做app,虽然以前自己是做app的测试的,但是好多东西都不是很明白,这里记录自己这段日子的历程。

1、搭建服务器端,以前做测试的时候,他们用的是Apache+mysql+php,而自己上学的时候接触过一些java的知识,就用java+servlet+jsp+Sqlserver+tomcat来做。

2、由于自己好长时间没有写代码了,所有写都是比较基础的例子。下面这个是根据百度文库来实现的android解析JSON数据的实例。

首先服务器端:

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 3     <display-name>ServletTest</display-name> 4     <welcome-file-list> 5         <welcome-file>index.html</welcome-file> 6         <welcome-file>index.htm</welcome-file> 7         <welcome-file>index.jsp</welcome-file> 8         <welcome-file>default.html</welcome-file> 9         <welcome-file>default.htm</welcome-file>10         <welcome-file>default.jsp</welcome-file>11     </welcome-file-list>12     13     14     <servlet>15     <servlet-name>Test</servlet-name>16     <servlet-class>com.android.servlet.ServletDemo</servlet-class>    17     </servlet>18     <servlet-mapping>19     <servlet-name>Test</servlet-name>20     <url-pattern>/test</url-pattern>21     </servlet-mapping>22 </web-app>

ServletDemo.class

 1 package com.android.servlet; 2  3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.List; 7  8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet;10 import javax.servlet.http.HttpServletRequest;11 import javax.servlet.http.HttpServletResponse;12 13 import net.sf.json.JSONArray;14 import net.sf.json.JSONObject;15 16 public class ServletDemo extends HttpServlet {17     18     private List<User> list;19     @Override20     protected void doGet(HttpServletRequest req, HttpServletResponse resp)21             throws ServletException, IOException {22         // TODO Auto-generated method stub23         24         25         resp.setContentType("text/plain");26         resp.setCharacterEncoding("utf-8");27         PrintWriter out=resp.getWriter();28         29         list=PrapareData();30 //        out.write(list.toString());40         41         42         JSONArray array=new JSONArray();43         44         for(User bean: list)45         {46             JSONObject obj=new JSONObject();47             48             try{49             50                 51                 obj.put("id", bean.getId());52                 obj.put("name", bean.getName());53                 obj.put("email", bean.getEmail());54                 obj.put("gender", bean.getGender());55                 56                 57             }58             catch(Exception e){59                 60             }61             62             63         array.add(obj);64             65             66         }67         out.write(array.toString());68 //        out.write("nihao!!!!");69         out.flush();70         out.close();71         72         73     }74     75     @Override76     protected void  doPost(HttpServletRequest req, HttpServletResponse resp)77             throws ServletException, IOException {78         // TODO Auto-generated method stub79         super.doGet(req, resp);80     }81     private List<User> PrapareData()82     {83         list=new ArrayList<User>();84         85         User user1=new User();86         user1.setId(1001);87         user1.setName("什么玩意儿");88         user1.setEmail("helloworld@163.com");89         user1.setGender("男");90         list.add(user1);91         92         return list;93     94     }95 96 }

User

 1 package com.android.servlet; 2  3 public class User { 4     private int id; 5     private String name; 6     private String email; 7     private String gender; 8     public int getId() { 9         return id;10     }11     public void setId(int id) {12         this.id = id;13     }14     public String getName() {15         return name;16     }17     public void setName(String name) {18         this.name = name;19     }20     public String getEmail() {21         return email;22     }23     public void setEmail(String email) {24         this.email = email;25     }26     public String getGender() {27         return gender;28     }29     public void setGender(String gender) {30         this.gender = gender;31     }32     33 34 }

在浏览器中输入地址:http://localhost:8080/ServletTest/test(备注,如果是局域网的其他电脑,把localhost改成要访问的那台的ip地址

[{"id":1001,"name":"什么玩意儿","email":"helloworld@163.com","gender":"男"}]
未完待续
一步一步地走来,慢慢地会好起来的,相信自己!

app开发历程---1,servlet 返回JSON作为android 接口实例