您的位置:首页 > 其它

消息推送结果封装

2017-03-16 00:00 309 查看

1 前言

在使用消息推送服务器,推送消息的返回结果可能不是我们想要的,这时候就得对消息返回结果进行封装。

2 使用反射进行封装

2.1 返回结果如下:

//返回成功格式:{taskId=OSS-0316_d261997c9778384b3520cbeb7c95cddc, result=ok, status=successed_offline}
//错误格式:{result=AppidError}

2.2 代码

//封装返回信息
//返回成功格式:{taskId=OSS-0316_d261997c9778384b3520cbeb7c95cddc, result=ok, status=successed_offline}
//错误格式:{result=AppidError}
GTResultVo gTResultVo = new GTResultVo();
String result = ret.getResponse().toString();
result = result.replace("{", "");
result = result.replace("}", "");
if(result.contains("result=ok")){
String[] attributeGroup = result.split(",");
for(String str : attributeGroup){
String attribute = str.trim();
if(StringUtils.areNotEmpty(attribute)){
String name = attribute.split("=")[0];
String value = attribute.split("=")[1];
try {
//首字母大写
Character c = Character.toUpperCase(name.charAt(0));
String classMethod = c.toString().concat(name.substring(1));
Method m = gTResultVo.getClass().getDeclaredMethod("set" + classMethod, String.class);
m.invoke(gTResultVo, value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
String name = result.split("=")[0];
String value = result.split("=")[1];
try {
//首字母大写
Character c = Character.toUpperCase(name.charAt(0));
String classMethod = c.toString().concat(name.substring(1));
Method m = gTResultVo.getClass().getDeclaredMethod("set" + classMethod, String.class);
m.invoke(gTResultVo, value);
} catch (Exception e) {
e.printStackTrace();
}
}

return gTResultVo;
/**
* @Title: GTResultVo.java
* @Package cc.messcat.common.vo
* @Description: TODO
* @author limh
* @date 2017年3月16日
*/
package cc.messcat.common.vo;
/**
* ClassName: GTResultVo
* @Description: 封装个推返回消息
* @author limh
* @date 2017年3月16日
*/
public class GTResultVo {
private String taskId;
private String result;
private String status;

/**
* @return the taskId
*/
public String getTaskId() {
return taskId;
}
/**
* @param taskId the taskId to set
*/
public void setTaskId(String taskId) {
this.taskId = taskId;
}
/**
* @return the result
*/
public String getResult() {
return result;
}
/**
* @param result the result to set
*/
public void setResult(String result) {
this.result = result;
}
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(String status) {
this.status = status;
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  消息推送