融雲接口調用示例(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){
    //自己系統的業務處理
}

注意:融雲的接口有時候會切換域名,一般會發郵件通知,記得修改,不然他們的接口會調不通。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章