畅言数据回推参数格式及JAVA解析代码
转自高光翔的blog:http://www.ggxblog.cn/ArticleIndex?id=9
畅言数据回推的官方文档可参考http://changyan.kuaizhan.com/static/help/b-push-back.html,然而官方接口文档写的并不是太详细,传的参数格式并没有很好的说明,用户根据官方文档并不能一次性的正确使用,还需要去试。我在使用的过程中刚开始就没能理解正确的参数格式,导致回推并未获取到数据。下面我将说明数据回推的参数格式以及用Java解析获取到回推数据。
官方文档是这么写的:畅言将以POST方式调用接口,参数名为data,值是一个字符串,可以解板为以下格式的json。
代码示例:
{
"title":"123", //文章标题
"url":"http://localhost/?p=9", //文章url
"ttime":1401327899094, //文章创建时间
"sourceid":"9", //文章Id
"parentid":"0", //文章所属专辑的ID,多个的话以,号分隔
"categoryid":"", //文章所属频道ID(可留空)
"ownerid":"", //文章发布者ID(可留空)
"metadata":"", //文章其他信息(可留空)
"comments":[
{
"cmtid":"358", //评论唯一ID
"ctime":1401327899094, //评论时间
"content":"2013年8月1日18:36:29 O(∩_∩)O~",//评论内容
"replyid":"0", //回复的评论ID,没有为0
"user":{
"userid":"1",//发布者ID
"nickname":"admin", //发布者昵称
"usericon":"",//发布者头像(留空使用默认头像)
"userurl":"", //发布者主页地址(可留空)
"usermetadata":{ //其它用户相关信息,例如性别,头衔等数据
"area": "北京市",
"gender": "1",
"kk": "",
"level": 1
}
},
"ip":"127.0.0.1",//发布ip
"useragent":"",//浏览器信息
"channeltype":"1",//1为评论框直接发表的评论,2为第三方回流的评论
"from":"", //评论来源
"spcount":"",//评论被顶次数
"opcount":"", //评论被踩次数
"attachment":[ //附件列表
{
"type":1,//1为图片、2为语音、3为视频
"desc":"",//描述,
"url":"http://img.sohu.itc/xxxx" //附件地址
}
]
}
]
}
刚开始我的理解是这样的
var param={
"title":"123", //文章标题
"url":"http://localhost/?p=9", //文章url
...
}
$.ajax({
…
data:JSON.stringify(param),
…
});
然而这样并没有正确的获取到数据。后面经过自己的试错,发现参数实际上是这个样子的
var param={
data:
"{\"sourceid\":\"G0120171110102726495\"," +
"\"comments\":[{" +
"\"content\":\"2018年7月15日18:36:29 O(∩_∩)O~\","+
"\"user\":{"+
"\"userid\":\"1\","+
"\"nickname\":\"西红柿炒恐龙蛋\", "+
"\"userurl\":\"\""+
"},"+
"\"attachment\":[{"+
"\"type\":1}]"+
"}]}"
}
$.ajax({
…
data: param,
…
});
现在我们知道了数据回推所传入的参数的正确的格式,那么下面Java后台代码如何去解析呢?解析方式应该有多种,那么下面我给出自己已经实现验证过的一种方法。首先需要按照官方给的参数格式一层层的去构建DTO实体类。代码如下。
public class PostReplyDTO {
String title;
String url;
String sourceid;
List<CommentsDTO> comments;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getSourceid() {
return sourceid;
}
public void setSourceid(String sourceid) {
this.sourceid = sourceid;
}
public List<CommentsDTO> getComments() {
return comments;
}
public void setComments(List<CommentsDTO> comments) {
this.comments = comments;
}
}
public class CommentsDTO {
private String cmtid;
private String content;
private String replyid;
private String ip;
/*
* 浏览器信息
*/
private String useragent;
private ReplyUserDTO user;
private List<AttachmentDTO> attachment;
public String getCmtid() {
return cmtid;
}
public void setCmtid(String cmtid) {
this.cmtid = cmtid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getReplyid() {
return replyid;
}
public void setReplyid(String replyid) {
this.replyid = replyid;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getUseragent() {
return useragent;
}
public void setUseragent(String useragent) {
this.useragent = useragent;
}
public ReplyUserDTO getUser() {
return user;
}
public void setUser(ReplyUserDTO user) {
this.user = user;
}
public List<AttachmentDTO> getAttachment() {
return this.attachment;
}
public void setAttachment(List<AttachmentDTO> attachment) {
this.attachment = attachment;
}
}
public class ReplyUserDTO {
private String userid;
private String nickname;
private String usericon;
private String userurl;
public String getUserurl() {
return userurl;
}
public void setUserurl(String userurl) {
this.userurl = userurl;
}
public String getUsericon() {
return usericon;
}
public void setUsericon(String usericon) {
this.usericon = usericon;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getNickname() {
return nickname;
}
}
public class AttachmentDTO {
Integer type;
String desc;
String url;
public void setType(Integer type) {
this.type = type;
}
public Integer getType() {
return this.type;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getDesc() {
return this.desc;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return this.url;
}
}
用户可根据自己需要的参数去去添加字段。另外我在使用的过程中发现需要构建Attachment的模型,不然会报错,即使我没有用到attachment中的数据。
下面就是解析jason字符串的代码了。
import net.sf.json.JSONObject;
@Controller
public class ArticleIndexController {
@RequestMapping(value = "/UpdateLatestReply")
public void UpdateLatestReply(PrintWriter printWriter, HttpServletRequest request) {
String callback = request.getParameter("callback");
String data = request.getParameter("data");
JSONObject jsonObject = JSONObject.fromObject(data);
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("comments", CommentsDTO.class);
PostReplyDTO replyDto =
(PostReplyDTO) JSONObject.toBean(jsonObject, PostReplyDTO.class, classMap);
String sourceid = replyDto.getSourceid();
List<CommentsDTO> commentsDTOs = replyDto.getComments();
String content = "";
String userid = "";
String nickname = "";
String usericon = "";
String userurl = "";
if (commentsDTOs.size() > 0) {
CommentsDTO commentsDTO = (CommentsDTO) commentsDTOs.get(0);
content = commentsDTO.getContent();
ReplyUserDTO replyUserDTO = commentsDTO.getUser();
if (replyUserDTO != null) {
userid = replyUserDTO.getUserid();
nickname = replyUserDTO.getNickname();
usericon = replyUserDTO.getUsericon();
userurl = replyUserDTO.getUserurl();
}
}
//自己的逻辑代码
...
int result = 1;
printWriter.write(callback + "(" + String.valueOf(result) + ")");
printWriter.flush();
printWriter.close();
}
解析JSON字符串用到了net.sf.json.JSONObject JAR包。另外需要注意的是传参的数据类型是jsonp格式的。这样就完成了数据回推的json数据解析和入库的工作。