**
后端根据用户信息获取融云token
**
集成融云需要后端把用户的id,头像,昵称发送到融云来获取融云的token,app端用获取到的融云token来连接融云。
献上代码!!!
private static final String App_Key = "****";
private static final String App_Secret = "****";
private static final String TIMESTAMP = "RC-Timestamp";
private static final String updateUrl = "http://api-cn.ronghub.com/user/refresh.json";
private static final String url = "http://api-cn.ronghub.com/user/getToken.json";
public static String getToken( String userId, String userHead, String userName) {
StringBuffer res = new StringBuffer();
String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳,从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。
String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数,无长度限制。
String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。
//Logger.i(Signature);
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("App-Key", App_Key);
httpPost.setHeader("Timestamp", Timestamp);
httpPost.setHeader("Nonce", Nonce);
httpPost.setHeader("Signature", Signature);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("name", userName));//名称(例如使用这个功能的‘张三’)
nameValuePair.add(new BasicNameValuePair("userId", userId));// 用户id(根据自己的项目,自己生成一个串就行,UUID就行)
nameValuePair.add(new BasicNameValuePair("portraitUri", userHead));//头像(存储头像的路径)
HttpResponse httpResponse = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));
httpResponse = httpClient.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String line = null;
while ((line = br.readLine()) != null) {
res.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// System.out.println("res=" + res.toString());
JSONObject jsonObject = JSON.parseObject(res.toString());
//Logger.i(userRespone.getCode()+"");
// System.out.println(jsonObject.getString("token"));
return jsonObject.getString("token");
}
融云的这个接口属于http协议,普通的http调用即可。