日期:2014-05-16 浏览次数:20432 次
doGet(request,response);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置字符编码 response.setContentType("text/plain;UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); //准备用户数据 DBInquire inquire=new DBInquire(); //进入数据库 DbEntrance mysql=new DbEntrance(); inquire.conn=mysql.getDBConnect(); list=inquire.selectUser(); //实例化一个JSON数据,用来存储用户数据 JSONArray array=new JSONArray(); //遍历链表,将数据一个个转移到JSON链表中 for(int i=0;i<list.size();i++){ System.out.println("打印出数据库中的数据:"+list.get(i).getUName()+"-=-="+list.get(i).getUPwd()); //实例化一个JSON对象,用来存储用户数据 JSONObject obj = new JSONObject(); //存储用户名 obj.put("User",list.get(i).getUName()); //存储用户密码 obj.put("Pwd", list.get(i).getUPwd()); //存储用户电话 obj.put("tel",list.get(i).getUNumber()); //存储用户电子邮件 obj.put("email", list.get(i).getUEmail()); //添加到JSON链表中 array.add(obj); } //输出 out.write(array.toString()); out.flush(); out.close(); }
<TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("-=-=-=-=-=-="); setContentView(R.layout.activity_main); System.out.println("8888888888888888888"); try{ StringBuffer sb = new StringBuffer(); //在测试过程中,经常是用本机做测试服务器,访问本机的IP地址要设置为10.0.2.2 String url = "http://10.0.2.2:8080/Coupons/JsonServletAndroid"; String body = getContent(url); JSONArray array = new JSONArray(body); System.out.println("所得Json的长度是:"+array.length()); for(int i=0; i<array.length(); i++){ System.out.println("标记符11111111111111111111"); JSONObject obj = array.getJSONObject(i); System.out.println("打印出第"+i+"个数据是:"+obj.getInt("User")+"-=-"+obj.getInt("Pwd")); sb.append("tel:").append(obj.getString("tel")).append("/r"+"/n"); sb.append("name:").append(obj.getString("User")).append("/r"+"/n"); sb.append("pwd:").append(obj.getString("Pwd")).append("/t"); sb.append("email:").append(obj.getString("email")).append("/r/n"); sb.