Java开发微博粉丝服务(2)——消息推送服务

本文详细介绍了如何使用Java开发微博粉丝服务平台的消息推送服务,包括接收和处理普通消息(如文本、位置、语音、图片)、事件消息(如事件、被@),以及发送被动响应(纯文本、图文、位置)。文章还提到了处理不同消息类型的代码实现,并讨论了相关数据封装和JSON转换的实现细节。
摘要由CSDN通过智能技术生成

第二部分——消息推送服务

类型一:接受普通消息

类型二:接受事件消息

类型三:发送被动响应

项目树图。

注意:这个部分所用到的jar包有fastjson-1.2.47.jar(版本自定义,本人用的1.2.47)

一、接受普通消息

1、纯文本类型私信和留言消息:text

{

    "type": "text",

    "receiver_id": 1902538057,

    "sender_id": 2489518277,

    "created_at": "Mon Jul 16 18:09:20 +0800 2012",

    "text": "私信或留言内容",

    "data": {}

}

返回值说明

属性

值的类型

说明描述

type

string

text

receiver_id

int64

消息的接收者

sender_id

int64

消息的发送者

created_at

string

消息创建时间

text

string

私信内容

data

string

消息内容,纯文本私信或留言为空

当用户向微博服务器发送消息,微博消息会推送到开发者填写的URL地址上(json),所以URL会返回一个json的数据包,获取消息就是将消息从该个json的数据包中提取出来。下边就在cn.json.weibo包下新建一个名位RequestMethod的java文件,该个类的作用是获取request返回的数据,代码具体内容如下:

RequestMethod.java

1.	package cn.json.weibo;  
2.	  
3.	public class RequestMethod {  
4.	    /** 
5.	     * 获取request返回的JSON字符串数据包 
6.	     */  
7.	    public static String getRequestJSON(HttpServletRequest request) throws IOException {  
8.	        String submitMehtod = request.getMethod();  
9.	        //判断request返回的方式是GET 还是 POST  
10.	        if (submitMehtod.equals("GET")) {  
11.	            return new String(request.getQueryString().getBytes("iso-8859-1"),"utf-8").replaceAll("%22", "\"");  
12.	        } else {  
13.	            return getRequestPost(request);  
14.	        }  
15.	    }  
16.	    /**       
17.	     * 获取POST 请求内容 ,并且返回一个字符串 
18.	     */  
19.	    public static String getRequestPost(HttpServletRequest request)throws IOException {  
20.	        byte buffer[] = getRequestPostBytes(request);  
21.	        String charEncoding = request.getCharacterEncoding();  
22.	        if (charEncoding == null) {  
23.	            charEncoding = "UTF-8";  
24.	        }  
25.	        return new String(buffer, charEncoding);  
26.	    }  
27.	    /**       
28.	     * 获取 POST 请求的 byte[] 数组    
29.	     */  
30.	    public static byte[] getRequestPostBytes(HttpServletRequest request)throws IOException {  
31.	        int contentLength = request.getContentLength();  
32.	        if(contentLength<0){  
33.	            return null;  
34.	        }  
35.	        byte buffer[] = new byte[contentLength];  
36.	        for (int i = 0; i < contentLength;) {  
37.	            int readlen = request.getInputStream().read(buffer, i, contentLength - i);  
38.	            if (readlen == -1) {  
39.	                break;  
40.	            }  
41.	            i += readlen;  
42.	        }  
43.	        return buffer;  
44.	    }  
45.	}  

②WeiBo.java下doPost方法内容如下:

1.   public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {  
2.           //设定字符集  
3.           request.setCharacterEncoding("UTF-8");  
4.           response.setCharacterEncoding("UTF-8");  
5.           PrintWriter out = response.getWriter();  
6.           //获取返回的数据  
7.           String requestJSON = RequestMethod.getRequestJSON(request);  
8.           System.out.println(requestJSON);  
9.           //将JSON字符串转换为集合  
10.         Map<?, ?> map = JSON.parseObject(requestJSON);  
11.         //获取字段为receiver_id的内容  
12.         String receiver_id = map.get("receiver_id").toString();  
13.         String sender_id = map.get("sender_id").toString();  
14.         String type = map.get("type").toString();  
15.         String text = map.get("text").toString();  
16.     }  

2、位置类型私信消息:position

{

    "type": "position",

    "receiver_id": 1902538057,

    "sender_id": 2489518277,

    "created_at": "Mon Jul 16 18:09:20 +0800 2012",

    "text": "我在这里: http://t.cn/zQgLLYO",

    "data": {

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用Java发送透传消息到个推,你需要使用个推提供的Java SDK。 以下是一个简单的示例代码,用于向单个设备发送透传消息: ``` import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.exceptions.PushException; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.TransmissionTemplate; public class PushExample { // Replace with your appID, appKey, masterSecret and deviceToken private static String appId = "your_appId"; private static String appKey = "your_appKey"; private static String masterSecret = "your_masterSecret"; private static String deviceToken = "your_deviceToken"; public static void main(String[] args) { IGtPush push = new IGtPush(appKey, masterSecret); TransmissionTemplate template = new TransmissionTemplate(); template.setTransmissionContent("your_transmission_content"); Target target = new Target(); target.setAppId(appId); target.setClientId(deviceToken); IPushResult result = null; try { result = push.pushMessageToSingle(template, target); } catch (PushException e) { e.printStackTrace(); } if (result != null) { System.out.println(result.getResponse().toString()); } else { System.out.println("Failed to send push notification"); } } } ``` 在此代码中,你需要用你的个推应用程序的实际值替换变量 `appId`、`appKey` 和 `masterSecret`。你还需要指定你要发送消息的设备的 `deviceToken`,以及你要发送的实际消息内容 `your_transmission_content`。 此代码使用透传模板 `TransmissionTemplate`,这意味着发送消息将不会在设备上显示,而是直接传递给应用程序进行处理。你可以自己定义透传消息的内容,以便你的应用程序可以根据需要进行处理。 当你运行此代码时,它将尝试将透传消息发送到指定的设备,并将响应打印到控制台。如果一切顺利,你应该看到推送成功的响应。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值