
2.2 综合案例:灵客
如何让初学Android编程的读者,能够在学习的过程中将各章节内容形成一个统一的有机整体,学习开发一个完整的应用,并掌握Android应用与网络中的服务器进行交互的技术,是本书要着力解决的问题之一。
为了降低读者学习的难度,对一款产品的开发有整体的理解,并能够尽快上手自己设计开发应用,本书采用将一个完整的综合案例贯穿全书的方法来讲述移动应用的开发过程,并将其功能需求的实现穿插到书中各章节。通过结合该案例给读者系统而生动地阐述各个知识点,并最终引领读者构建一款完整的移动应用产品。
这个综合案例来自于企业实际开发的互联网产品,并根据需要进行了适当的调整和简化,以利于学习。本书将这个综合案例命名为“灵客”。灵客是一款类似于微博的互联网应用产品,为了便于读者对灵客这个案例的功能有一个全面的认识,以便于后续章节的学习,接下来从“灵客功能需求”和“灵客设计概要”两个方面对案例进行描述。
读者在学习的过程中也可以先粗略了解本节内容,在后续章节的学习过程中根据需要再翻阅本节相应的内容。
2.2.1 灵客功能需求
灵客主要包含发布话题、评论、好友资料查看、私信等功能。本书将从基础版块、操作功能、业务逻辑三方面来介绍灵客的功能需求。
1.基础版块
如图2.14所示,该案例将包含四大基础版块的内容:用户个人资料,话题,私信,好友。

图2.14 基础版块
各版块包含的内容如图2.15~图2.18所示。

图2.15 用户个人资料

图2.16 话题

图2.17 私信

图2.18 好友
2.操作功能
如图 2.19 所示,该综合案例将包含注册、登录、退出、注销、发表/查看话题、评论话题、发送/接收私信、阅读私信、添加/删除好友以及查看用户资料等功能。
3.业务逻辑
如图2.20所示,该综合案例包括如下业务逻辑。
(1)要使用该产品的功能,需要经过有效的注册。
(2)该产品的所有用户都可以发表话题,并且发表的话题能够让本产品所有用户看见,用户还可以回复话题。
(3)本产品所有的用户都能够给任意其他用户发送文本私信,并且保留发送记录。
(4)本产品的所有用户都能够接收任意其他用户发送的文本私信,并且可以进行阅读和查看接收记录。
(5)对于本用户而言,发送或者接收的私信内容都是可以查看的。
(6)任意用户可被其他用户加为好友,前提是同意其他用户所发送的加为好友的消息。
(7)两用户可以解除好友关系,产品将通过一定方式把好友关系中解除一方的操作告知被解除一方。
(8)使用本产品的用户可以查看其他用户的资料。

图2.19 操作功能

图2.20 业务逻辑
2.2.2 灵客设计概要
本设计概要主要针对Android客户端,包含总体设计、数据库表设计和网络接口设计。
1.总体设计
如图2.21所示,灵客在Android客户端上的设计主要包含四大部分。

图2.21 总体设计
(1)用户界面,即UI方面的设计,对应第5章的内容。
(2)数据库,对应第8章的内容。
(3)事件处理,包含常用的事件响应机制和处理流程,主要对应第4章、第6章、第7章、第10章的内容。
(4)网络访问,对应第9章的内容。
2.数据库表设计
数据库表设计如表2.3至表2.5所示。
表2.3 Topic(话题)表

表2.4 PrivateLetter(私信)表

表2.5 Friend(好友信息)表

续表

3.网络接口设计
(1)请求参数。本案例将采用JSON格式进行传输,基本参数包括用户名、密码和请求数据。其中请求数据有随机字符串、请求类型、请求参数。请求参数以数组形式传递。其数据格式如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx"
"requestType":"xxx",
"params":["xxx","xxx","xxx"]
}
(2)返回参数。其数据格式如下。
{
"result":"xxx",
"requestType":"xxx",
"content":[ { "xxx","xxx",...},...]
}
请求参数和返回参数的requestType需一致。result的数字表示返回结果,1为成功,0为失败。content中的内容返回的是JSON格式的字符串。下面将给出在本书综合案例中会使用到的19种请求和返回参数的设计。
① 登录(Login)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"Login",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":"Login"
"content":[用户个人资料]
}
② 注销(Logout)
请求数据如下。
{
"username":"xxx",
"password":"xxx〞,
"randomkey":"xxx",
"requestType":"Logout",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" Logout"
"content":[]
}
③ 注册(Signin)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"Signin",
"params":[UID,姓名,密码,手机号,头像,性别,地址]
}
返回数据如下。
{
"result":"数字"
"requestType":" Signin"
"content":[]
}
④ 添加好友(AddFriend)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"AddFriend",
"params":["好友ID,好友ID,..."]
}
返回数据如下。
{
"result":"数字"
"requestType":"AddFriend"
"content":[]
}
⑤ 发表话题(AddTopic)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"AddTopic",
"params":["话题内容","话题发送时间","话题名字","话题包含的图片"]
}
返回数据如下。
{
"result":"数字"
"requestType":"AddTopic"
"content":[]
}
⑥ 添加话题回复(AddTopicComment)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"AddTopicComment",
"params":["话题ID","评论内容","评论的图片","评论时间","评论ID"]
}
返回数据如下。
{
"result":"数字"
"requestType":"AddTopicComment"
"content":["评论ID"]
}
⑦ 删除好友(DeleteFriend)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"DeleteFriend",
"params":[好友ID]
}
返回数据如下。
{
"result":"数字"
"requestType":"DeleteFriend"
"content":[]
}
⑧ 删除私信(DeletePrivateLetter)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"DeletePrivateLetter",
"params":[私信ID]
}
返回数据如下。
{
"result":"数字"
"requestType":"DeletePrivateLetter"
"content":[]
}
⑨ 获得所有好友(GetAllFriends)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetAllFriends",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":"GetAllFriends"
"content":[
{
"personAddress":"xxx",
"personName":"xxx",
"personSex":"xxx",
"personMobile":"xxx"
"personPhoto":"xxx"
}
,
{
"personAddress":"xxx",
"personName":"xxx",
"personSex":"xxx",
"personMobile":"xxx"
"personPhoto":"xxx"
}
...
]
}
⑩ 获得所有私信(GetAllPrivateLetter)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"GetAllPrivateLetter",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":"GetAllPrivateLetter"
"content":[
{
"privateLetterIsSend":"xxx",
"privateLetterID":"xxx",
"privateLetterName":"xxx",
"privateLetterContent":"xxx",
"UID":"xxx",
"privateLetterPhoto":"xxx",
"privateLetterUID":"xxx"
}
,
{
"privateLetterIsSend":"xxx",
"privateLetterID":"xxx",
"privateLetterName":"xxx",
"privateLetterContent":"xxx",
"UID":"xxx",
"privateLetterPhoto":"xxx",
"privateLetterUID":"xxx"
}
...
]
}
⑪获得所有话题(GetAllTopic)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetAllTopic",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetAllTopic"
"content":[
{
"topicID":"xxx",
"topicUID":"xxx",
"topicContent":"xxx",
"topicTime":"xxx",
"topicName":"xxx",
"topicPhoto":"xxx"
}
,
{
"topicID":"xxx",
"topicUID":"xxx",
"topicContent":"xxx",
"topicTime":"xxx",
"topicName":"xxx",
"topicPhoto":"xxx"
}
...
]
}
⑫获得所有删除的好友(GetAllDeleteFriends)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetAllDeleteFriends",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetAllDeleteFriends"
"content":[删除好友的ID,删除好友的ID,...]
}
⑬获得所有新增加的好友(GetNewFriends)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetNewFriends",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetNewFriends"
"content":[新友的ID,新友的ID,...]
}
⑭获得所有新私信(GetNewPrivateLetter)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetNewPrivateLetter",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetNewPrivateLetter"
"content":[{
"privateLetterIsSend":"xxx",
"privateLetterID":"xxx",
"privateLetterName":"xxx",
"privateLetterContent":"xxx",
"UID":"xxx",
"privateLetterPhoto":"xxx",
"privateLetterUID":"xxx"
}
,
{
"privateLetterIsSend":"xxx",
"privateLetterID":"xxx",
"privateLetterName":"xxx",
"privateLetterContent":"xxx",
"UID":"xxx",
"privateLetterPhoto":"xxx",
"privateLetterUID":"xxx"
}
...]
}
⑮获得新话题(GetNewToipc)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetNewTopic",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetNewTopic"
"content":[
{
"topicID":"xxx",
"topicUID":"xxx",
"topicContent":"xxx",
"topicTime":"xxx",
"topicName":"xxx",
"topicPhoto":"xxx"
}
,
{
"topicID":"xxx",
"topicUID":"xxx",
"topicContent":"xxx",
"topicTime":"xxx",
"topicName":"xxx",
"topicPhoto":"xxx"
}
...
]
}
⑯获得某用户的资料(GetPersonInfo)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetPersonInfo",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetPersonInfo"
"content":[
{
"personAddress":"xxx",
"personName":"xxx",
"personSex":"xxx",
"personMobile":"xxx"
"personPhoto":"xxx"
}
]
}
⑰获得某用户的状态(GetPersonStatus)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":" GetPersonStatus",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":" GetPersonStatus"
"content":[
{
"UID":"xxx",
...
}
]
}
⑱获得话题评论(GetTopicComment)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"GetTopicComment",
"params":[]
}
返回数据如下。
{
"result":"数字"
"requestType":"GetTopicComment"
"content":[
{
"Topic_Com_ID":"xxx",
"Topic_Com_Content":"xxx",
"Topic_Com_Photo":"xxx",
"Topic_Com_Time":"xxx",
"Topic_Com_From":"xxx"
}
,
{
"Topic_Com_ID":"xxx",
"Topic_Com_Content":"xxx",
"Topic_Com_Photo":"xxx",
"Topic_Com_Time":"xxx",
"Topic_Com_From":"xxx"
}
...
]
}
⑲发送私信(SendPrivateLetter)
请求数据如下。
{
"username":"xxx",
"password":"xxx",
"randomkey":"xxx",
"requestType":"SendPrivateLetter",
"params":[用户id,内容,时间,私信名,图片]
}
返回数据如下。
{
"result":"数字"
"requestType":"SendPrivateLetter"
"content":[私信id]
}
这些接口表示了本书的综合案例“灵客”所构建的产品与服务器之间进行交互的格式,客户端和服务端之间的信息交互涉及网络和协议,在第9章Android网络通信编程中将做详细讲解。