食堂APP-项目开发及踩坑记录(4)

进入最主要的功能开发环节:搜索

我们组采用基于servlet为服务器的中转实现方式,其主要的思路为:通过servlet搭建的服务器将mysql中需要的数据取出来并转换为json数据,安卓端调用servlet的json数据并进行解析

因为APP是对数据进行展示的,服务端则是对数据进行处理返回,有点像WEB的前后端概念,开发进入到前后端分离状态,服务器端主要是利用web中的servelt  

在现阶段APP开发与接口并行,在没有数据时考虑模拟接口或者借用接口来完成开发发测试,等接口开发完毕进行更改调试

这里贴一些web端生成json数据的代码

要利用到gson的包,这里dao层的函数就是简单的一个搜索,然后将数据传入list后在servlet层转换成json数据

package com.fin.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//import javax.smartcardio.ResponseAPDU;
import com.fin.bean.yq;
import com.fin.dao.CourseDao;
import com.google.gson.Gson;
/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/CourseServlet")
public class CourseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CourseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        
           this.list(request,response);
        
        
    }

    


    

    

    

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO 自动生成的方法存根
        //yq cour = new yq();
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        /*response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type","text/html;charset=UTF-8");*/
        String Date = request.getParameter("time");
        List<yq> list =  CourseDao.list(Date);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        System.out.println(json);
        response.getWriter().write(json);
    

        
    }
    

    

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        
    }
}
    


    

    

    
View Code

 

 

posted @ 2020-04-20 10:33  抽象博客  阅读(207)  评论(0编辑  收藏  举报