融云接口调用示例(http)

    项目中要用到融云的接口,又不想引融云的sdk,所以就直接通过http请求的方式来调用融云提供的三方接口。

首先,调用融云接口得获取他们的App Key / Secret,App Key / Secret 相当于在融云的帐号和密码。是融云 SDK 连接服务器所必需的标识,每一个 App 对应一套 App Key / Secret。所以我们一般要申请两套,测试环境和生产环境,开发的时候用测试的即可。

再者,我们需要配置下需要调用的融云接口,可以以多种方式,如xml,properties等,我这里直接定义的java常量。

最后,废话不多说,直接上代码。

public static final String RONG_URL_PRIFX = "http://api-cn.ronghub.com";//融云接口地址

public static final String URL_BLOCK = URL_PRIFX + "/user/block.json";//融云具体业务接口

public static final int BLOCK_TIME = 43200;

//event是我自己业务处理的对象,你们可以忽略
HashMap<String, Object> eventData = (HashMap<String, Object>) event.getData();
if (eventData == null)
{
  return;
}
Long userId = StringUtil.toLong(eventData.get("userId"));
String userIdValue = event.getTenantId() + "_" + userId;
String noncestr = StringUtil.random(16);
String timeStamp = StringUtil.valueOf(new Date().getTime());
StringBuilder toSign = new StringBuilder(RongCloudConst.APP_SECRET).append(noncestr).append(timeStamp);//APP_SECRET
List<BasicNameValuePair> formParams = new ArrayList<BasicNameValuePair>();
formParams.add(new BasicNameValuePair("userId",userIdValue));
formParams.add(new BasicNameValuePair("minute",RongCloudConst.BLOCK_TIME + ""));
HttpPost post = new HttpPost(RongCloudConst.URL_BLOCK);
post.addHeader("RC-App-Key", RongCloudConst.APP_KEY);//融云的app_key
post.addHeader("RC-Nonce", noncestr);
post.addHeader("RC-Timestamp", timeStamp);
post.addHeader("RC-Signature", RongCloudSHA1.getSHA1(toSign.toString()));
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
post.setEntity(new UrlEncodedFormEntity(formParams, Consts.UTF_8));
String rsp = HttpUtil.excuteRequest(post, true);
JSONObject res = JSONObject.parseObject(rsp);
if(res!=null && res.getIntValue("code") == 200){
    //自己系统的业务处理
}

注意:融云的接口有时候会切换域名,一般会发邮件通知,记得修改,不然他们的接口会调不通。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值