对接天猫接口之获取天猫订阅消息(Java实现)

最近对接了天猫接口,先说一下如何对接天猫订阅消息。

对接天猫订阅消息步骤:

1.在天猫开通对接接口权限,怎么开通直接联系天猫小二就行。不同的类目开通流程有所不同。

2.获取天猫的appkey, appSecret

3.天猫中开通订阅消息

4.调用端,开通订阅消息授权

    public String tmcUserPermit() {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, appSecret);
        TmcUserPermitRequest req = new TmcUserPermitRequest();
        req.setTopics("tmall_fuwu_AnomalyRecourse,tmall_fuwu_AnomalyRecourseStatusUpdate,tmall_fuwu_SettleAdjustmentStatusUpdate");
        TmcUserPermitResponse response;
        try {
            response = client.execute(req, sessionkey);
            return response.getBody();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return "没有获取到数据";
    }

注意:

1)以前授权的订阅消息,不能删掉,删掉后表示取消以前的订阅消息;

2)多个订阅消息间,用逗号分开

5.编写代码接受和解析订阅消息

Java实现:

    public void tmall_fuwu_WorkcardInfo_SDK() throws Exception {
        TmcClient client = new TmcClient(wsurl, appkey, appSecret, "default");//
        client.setMessageHandler(new MessageHandler() {
            public void onMessage(Message message, MessageStatus status) {
                try {                    
                    String topic=message.getTopic();
                    String context = message.getContent();
                    if ("tmall_fuwu_WorkcardInfo".equalsIgnoreCase(topic)) {                    
                        context = context.replace("\\\"", "'");
                        JsonConverter json = new JsonConverter();
                        ServiceTaskReponse serviceTask = json.toResponse(
                                context, ServiceTaskReponse.class);                    
                        System.out.println(serviceTask);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    status.fail();//
                }
            }
        });
        client.connect();
        Thread.sleep(10000);
    }

其中:

 status.fail();这行代码很关键,表示告知天猫接受失败,天猫会自动重新传输。

下载源码

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋9

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值