美文网首页Android开发Java服务器端编程Android开发
在线答题系统(服务端,客户端包括web和android)、( 二

在线答题系统(服务端,客户端包括web和android)、( 二

作者: 根艮哏艮根 | 来源:发表于2018-06-08 14:58 被阅读159次

    接着上篇文章我们继续,上篇文章主要写了数据库设计,后台功能的设计,相对来说比较简单,这篇文章我们就来写前台页面的编写以及android端的实现,接口的编写。

    一、接口的编写
    我们需要将题干和选项发送给客户端,这就需要我们将这两个信息拼接为json字符串发送到前台(客户端)。
    拼接效果为:

    {
        "count": 4,
        "code": 200,
        "msg": "成功",
        "data": [
            {
                "title": {
                    "id": 19,
                    "op_id": 19,
                    "content": "现在我们看到的万里长城是( )时期留下来的",
                    "sub_id": 7,
                    "status": 1
                },
                "option": [
                    {
                        "id": 18,
                        "t_id": 19,
                        "iden": "A",
                        "content": "秦朝",
                        "status": 1
                    },
                    {
                        "id": 19,
                        "t_id": 19,
                        "iden": "B",
                        "content": "明朝",
                        "status": 1
                    },
                    {
                        "id": 20,
                        "t_id": 19,
                        "iden": "C",
                        "content": "清朝",
                        "status": 1
                    },
                    {
                        "id": 21,
                        "t_id": 19,
                        "iden": "D",
                        "content": "唐朝",
                        "status": 1
                    }
                ]
            },
            {
                "title": {
                    "id": 20,
                    "op_id": 23,
                    "content": "杜牧有诗句“一骑红尘妃子笑”,其中“妃子”是()11",
                    "sub_id": 7,
                    "status": 1
                },
                "option": [
                    {
                        "id": 22,
                        "t_id": 20,
                        "iden": "A",
                        "content": "武则天",
                        "status": 1
                    },
                    {
                        "id": 23,
                        "t_id": 20,
                        "iden": "B",
                        "content": "杨贵妃",
                        "status": 1
                    },
                    {
                        "id": 24,
                        "t_id": 20,
                        "iden": "C",
                        "content": "慈溪",
                        "status": 1
                    },
                    {
                        "id": 25,
                        "t_id": 20,
                        "iden": "D",
                        "content": "花木兰",
                        "status": 1
                    }
                ]
            },
            {
                "title": {
                    "id": 21,
                    "op_id": 29,
                    "content": "古代科举考试最后在殿试中考取第二名别称为()",
                    "sub_id": 7,
                    "status": 1
                },
                "option": [
                    {
                        "id": 26,
                        "t_id": 21,
                        "iden": "A",
                        "content": "执牛耳",
                        "status": 1
                    },
                    {
                        "id": 27,
                        "t_id": 21,
                        "iden": "B",
                        "content": "状元",
                        "status": 1
                    },
                    {
                        "id": 28,
                        "t_id": 21,
                        "iden": "C",
                        "content": "探花",
                        "status": 1
                    },
                    {
                        "id": 29,
                        "t_id": 21,
                        "iden": "D",
                        "content": "榜眼",
                        "status": 1
                    }
                ]
            },
            {
                "title": {
                    "id": 27,
                    "op_id": 32,
                    "content": "文房四宝是谁发明的",
                    "sub_id": 7,
                    "status": 1
                },
                "option": [
                    {
                        "id": 30,
                        "t_id": 27,
                        "iden": "A",
                        "content": "乐毅",
                        "status": 1
                    },
                    {
                        "id": 31,
                        "t_id": 27,
                        "iden": "B",
                        "content": "闻仲",
                        "status": 1
                    },
                    {
                        "id": 32,
                        "t_id": 27,
                        "iden": "C",
                        "content": "蒙恬",
                        "status": 1
                    },
                    {
                        "id": 33,
                        "t_id": 27,
                        "iden": "D",
                        "content": "刘伯温",
                        "status": 1
                    }
                ]
            }
        ]
    }
    

    拼接关键代码如下:

      //从数据库中查询出该科目下的所有题目信息
    List<Map<String, Object>> titles=(List<Map<String, Object>>)iDao.getTitleContentByIndex(subId,indexs.get(i));
    
    for (int i = 0; i < titles.size(); i++) {
                String tId = titles.get(i).get("id").toString();
                // 查询选项信息
                options = (List<Map<String, Object>>) iDao.getOptionContent(tId);
                map = new HashMap<String, Object>();
                //拼接map
                map.put("title", titles.get(i));
                map.put("option", options);
                contents.add(map);
            }
    

    自己封装的json格式生成类:

    /**  
    * @ClassName: JsonBean  
    * @author Liu_xg  
    * @date 2018年5月7日  
    * @Description: TODO  layui中的表格数据封装javabean
    */
    public class JsonBeanUtils {
        
         /**{
              "count": 49416,
              "code": 0,
              "msg": null,
              "data": [
                {
                  "date": "2017-09-20",
                  "uv": 41,
                  "datatype": "mon",
                  "shopid": 0,
                  "id": 5,
                  "aid": 289714
                }
                
              ]
            }
        */
        /**
         * 状态码,成功:0,失败:其他
         */
        private int count;
        /**
         * 总数(分页)
         */
        private int code;
        /**
         * 错误提示
         */
        private String msg;
        /**
         * 数据集合
         */
        private List<?> data;
    
        /**
         * @return the count
         */
        public int getCount() {
            return count;
        }
    
        /**
         * @param count the count to set
         */
        public void setCount(int count) {
            this.count = count;
        }
    
        /**
         * @return the code
         */
        public int getCode() {
            return code;
        }
    
        /**
         * @param code the code to set
         */
        public void setCode(int code) {
            this.code = code;
        }
    
        /**
         * @return the msg
         */
        public String getMsg() {
            return msg;
        }
    
        /**
         * @param msg the msg to set
         */
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        /**
         * @return the data
         */
        public List<?> getData() {
            return data;
        }
    
        /**
         * @param data the data to set
         */
        public void setData(List<?> data) {
            this.data = data;
        }
    }
    
    

    这样就可以生成像刚开始的那种josn字符串
    服务端代码和web端代码需要的童鞋请私信我

    下载源码请点击这里(在线答题系统(服务端+客户端(pc端)))
    下载源码请点击这里(在线答题系统(Android端))

    相关文章

      网友评论

      • 晚夜星辰:代码不是全的吗?我看代码挺全的呀?
        根艮哏艮根:@leohoho 代码就是全的
      • 小小程序员jh:下载安卓端可以直接答题么?还是自己需要配置后台服务啊?
        根艮哏艮根:@小小程序员jh 我用的是eclispe 写的,tomcat8.0
        小小程序员jh:@根艮哏艮根 后台ide用什么配置,intellj可以么?
        根艮哏艮根:@小小程序员jh 你需要将后台程序也跑起来,不然没法答题的,后台服务很好配置的
      • 青青小青青:下载了你的Android源码,有没有数据库?
        根艮哏艮根:@青青小青青 数据库有啊,我导出来了.sql文件了

      本文标题:在线答题系统(服务端,客户端包括web和android)、( 二

      本文链接:https://www.haomeiwen.com/subject/kxrasftx.html