公司的项目app端需要用融云去做聊天那里类的东西,前台需要后台提供众多接口,找了好多帖子都没有java的demo,包括融云的官网里面也没有看到,最后没办法找了个安卓的demo改了改然后我们调试了一下也没有问题,代码详情看下面

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.*;public class rongyunController {private String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳,从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。private String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数,无长度限制。/*** @return* @Description 获取融云token* @param userId  自定义id串,我们用的是员工id* @param userHead  图片的url* @param userName  名称*/@RequestMapping(value = "/getToken", method = RequestMethod.POST)public Result<UserRespone> getToken(@RequestParam(required = true) String userId,@RequestParam(required = true) String userHead,@RequestParam(required = true) String userName) {StringBuffer res = new StringBuffer();String url = "http://api-cn.ronghub.com/user/getToken.json";String App_Key = "***********"; //开发者平台分配的 App Key。String App_Secret = "************";//开发者平台分配的App_Secret 。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());UserRespone userRespone = JSON.parseObject(res.toString(), UserRespone.class);//Logger.i(userRespone.getCode()+"");return new ResultUtil<UserRespone>().setData(userRespone);}/*** 查询群组成员* @param groupId 群组id* @return*/@ApiOperation(value = "查询群组成员")@RequestMapping(value = "/queryGroupMembers", method = RequestMethod.POST)public JSONObject queryGroupMembers(@RequestParam(required = true) String groupId){StringBuffer res = new StringBuffer();JSONObject jsonObject=new JSONObject();String url = "http://api-cn.ronghub.com/group/user/query.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。//Logger.i(Signature);//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient = HttpClientBuilder.create().build();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("groupId", groupId));// 群组的IdHttpResponse 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.fromObject(res.toString());//Logger.i(userRespone.getCode()+"");return jsonObject;}/*** 创建群组并添加组员* @param createStaffId 创建群组的员工id* @param joinSta  多个群组成员的id用逗号隔开拼接的字符串* @param groupHead  群组头像* @param groupName  群组名称* @return*/@ApiOperation(value = "创建群组并添加组员")@RequestMapping(value = "/groupCreateAndjoin", method = RequestMethod.POST)public Result<Map<String,Object>> groupCreateAndjoin(@RequestParam(required = true) String createStaffId,@RequestParam(required = true) String joinSta,@RequestParam(required = true) String groupHead,@RequestParam(required = true) String groupName){String [] joinStaffId=joinSta.split(",");Map<String,Object> map=new HashMap<String, Object>();//创建人id不能存在与joinSta 参数中for (String join:joinStaffId){if(createStaffId.equals(join)){return new ResultUtil<Map<String,Object>>().setAppErrorMsg("添加群组失败,创建人不能存在与群组成员中");}};//创建群组的urlString url = "http://api-cn.ronghub.com/group/create.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。HttpClient httpClient = HttpClientBuilder.create().build();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("userId", createStaffId));//要加入群的用户Id(创建这个群组的员工id)String groupId= UUID.randomUUID().toString().replace("-", "");//自定义生成群idnameValuePair.add(new BasicNameValuePair("groupId",groupId));// 创建群组 Id(根据自己的项目,我们采用创建人的"sz"+员工id)//获取创建人的姓名/*String groupName=staffMapper.selectStaffDetail(createStaffId).getStaffName();for (int i=0;i<joinStaffId.length;i++){if (i==1){Staff staff1=staffMapper.selectStaffDetail(joinStaffId[i]);groupName=groupName+","+staff1.getStaffName()+"等";break;}else {Staff staff1=staffMapper.selectStaffDetail(joinStaffId[i]);groupName=groupName+","+staff1.getStaffName();}}*/nameValuePair.add(new BasicNameValuePair("groupName", groupName));//群组Id对应的名称(我们采取创建人名字+两位成员名字+“等” 为群名称)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;String msg=null;String rongyunCode=null;while ((line = br.readLine()) != null) {System.out.println("line=" + line.toString());JSONObject jsonObject = JSONObject.fromObject(line); //将字符串类型的json格式串转换成json格式rongyunCode=jsonObject.get("code").toString();//获取融云返回的code码//创建分组成功后,直接添加分组成员if(rongyunCode.equals("200")){//添加成员的urlString url1 = "http://api-cn.ronghub.com/group/join.json";//Logger.i(Signature);//HttpClient httpClient1 = new DefaultHttpClient(); 废弃的方法,用下面的HttpClient httpClient1 = HttpClientBuilder.create().build();HttpPost httpPost1 = new HttpPost(url1);httpPost1.setHeader("App-Key", App_Key);httpPost1.setHeader("Timestamp", Timestamp);httpPost1.setHeader("Nonce", Nonce);httpPost1.setHeader("Signature", Signature);httpPost1.setHeader("Content-Type", "application/x-www-form-urlencoded");List<NameValuePair> nameValuePair1 = new ArrayList<NameValuePair>(1);for (String str:joinStaffId){Staff staff=staffMapper.selectStaffDetail(str);nameValuePair1.add(new BasicNameValuePair("userId", str));//要加入群的用户 Id,可提交多个,最多不超过 1000 个(员工id)nameValuePair1.add(new BasicNameValuePair("groupId", groupId));// 要加入的群组 Id(上面创建的群组id)nameValuePair1.add(new BasicNameValuePair("groupName", groupName));// 要加入的群 Id 对应的名称httpPost1.setEntity(new UrlEncodedFormEntity(nameValuePair1, "utf-8"));httpResponse = httpClient1.execute(httpPost1);BufferedReader br1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));String line1 = null;//String rongyunCode1=null;while ((line1 = br1.readLine()) != null) {System.out.println("line1=" + line1.toString());JSONObject jsonObject1 = JSONObject.fromObject(line1);rongyunCode=jsonObject1.get("code").toString();if(rongyunCode.equals("200")){msg="创建分组并添加成员成功";}else {msg=staff.getStaffName()+"添加群组失败,失败原因为:"+codeMsg(rongyunCode);map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组idmap.put("groupName",groupName);//群组名称return new ResultUtil<Map<String,Object>>().setData(map);}}}}else { //如果失败直接返回code码和msg值,不在操作添加msg=codeMsg(rongyunCode);}//res.append(line);}//群组创建成功后需要在融云群组信息表中添加一条数据if(rongyunCode.equals("200")){//创建分组成功后要发送群系统消息,不管此操作成不成功,不影响后续操作String types="0";String [] addStaffId=new String[0];//没用String [] addStaffName=new String[0];//没用groupPublish(createStaffId,addStaffId,addStaffName,groupId,types,groupName);//这里是我们自己的业务逻辑,主要是用来存储群主id和群头像的作用,不需要可忽略RongyunGroup rongyunGroup=new RongyunGroup();rongyunGroup.setGroupId(groupId); //群组idrongyunGroup.setGroupName(groupName);//群组名称rongyunGroup.setGroupHead(groupHead);//群组头像rongyunGroup.setGroupOwnerId(createStaffId);//群主idint res=rongyunGroupService.insertRongyunGroup(rongyunGroup);}map.put("rongyunCode",rongyunCode); //融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组idmap.put("groupName",groupName);//群组名称} catch (IOException e) {e.printStackTrace();}//Logger.i(userRespone.getCode()+"");return new ResultUtil<Map<String,Object>>().setData(map);}/*** 在已有的分组中添加分组成员* @param groupId 群组id* @param groupName 群组名称* @param joinSta 需要添加的组员id字符串,多个用逗号隔开* @param groupHead 群组头像* @return*/@RequestMapping(value = "/joinGroupMembers",method = RequestMethod.POST)@ApiOperation(value = "在已有的分组中添加分组成员")public Result<Map<String,Object>> joinGroupMembers(@RequestParam(required = true) String groupId,@RequestParam(required = true) String groupName,@RequestParam(required = true) String joinSta,@RequestParam(required = true) String groupHead){Map<String,Object> map=new HashMap<>();String [] joinStaffId=joinSta.split(",");//添加成员的urlString url = "http://api-cn.ronghub.com/group/join.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。//Logger.i(Signature);//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient = HttpClientBuilder.create().build();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);for (String str:joinStaffId){Staff staff=staffMapper.selectStaffDetail(str);nameValuePair.add(new BasicNameValuePair("userId", str));//要加入群的用户 Id,可提交多个,最多不超过 1000 个(员工id)nameValuePair.add(new BasicNameValuePair("groupId", groupId));// 要加入的群组 Id(上面创建的群组id)nameValuePair.add(new BasicNameValuePair("groupName", groupName));// 要加入的群 Id 对应的名称HttpResponse httpResponse=null;try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));httpResponse = httpClient.execute(httpPost);BufferedReader br1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));String line1 = null;String rongyunCode=null;String msg=null;//String rongyunCode1=null;while ((line1 = br1.readLine()) != null) {System.out.println("line1=" + line1.toString());JSONObject jsonObject1 = JSONObject.fromObject(line1);rongyunCode=jsonObject1.get("code").toString();if(rongyunCode.equals("200")){msg="添加成员成功";//添加成员成功后要发送群系统消息,不管此操作成不成功,不影响后续操作String types="2";String [] addStaffId=new String[]{str};//操作人idString [] addStaffName=new String[]{staff.getStaffName()};groupPublish(str,addStaffId,addStaffName,groupId,types,groupName);//**********成功后更新头像,groupName在这里没用,充数放进去的  start//获取创建人的姓名和成员名称拼新的名称,主要作用于一开始只有两个人的情况下,新增一个组员名称会变化int res=updateRongyunGroup(groupHead,groupName,groupId);if (res<=0){return new ResultUtil<Map<String,Object>>().setAppErrorMsg("群组添加成员成功但修改头像和名称失败");}//**********成功后更新头像,groupName在这里没用,充数放进去的   end}else {msg=staff.getStaffName()+"添加群组成员失败,失败原因为:"+codeMsg(rongyunCode);map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组idmap.put("groupName",groupName);//群组名称return new ResultUtil<Map<String,Object>>().setData(map);}}map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组idmap.put("groupName",groupName);//群组名称}catch (IOException e){e.printStackTrace();}}return new ResultUtil<Map<String,Object>>().setData(map);}/*** 退出群组 (自己的业务逻辑中不允许群主退出群组)* @param staffId 退出群的员工id,多个用逗号隔开* @param groupId 群组id* @param groupName  群组名称* @param groupHead  群组头像* @param type  1为踢人 其他为退群* @return*/@RequestMapping(value = "/quitGroupMembers",method = RequestMethod.POST)@ApiOperation(value = "退出群组")public Result<Map<String,Object>> quitGroupMembers(@RequestParam(required = true) String staffId,@RequestParam(required = true) String groupId,@RequestParam(required = true) String groupName,@RequestParam(required = true) String groupHead,@RequestParam(required = true) String type){Map<String,Object> map=new HashMap<>();//组内成员退出的urlString url = "http://api-cn.ronghub.com/group/quit.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。//Logger.i(Signature);//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient = HttpClientBuilder.create().build();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);String [] sta=staffId.split(",");for (String str:sta){nameValuePair.add(new BasicNameValuePair("userId", str));//要退出群的用户 Id(员工id)}//nameValuePair.add(new BasicNameValuePair("userId", staffId));//要退出群的用户 Id(员工id)nameValuePair.add(new BasicNameValuePair("groupId", groupId));// 要退出的群 IdHttpResponse httpResponse=null;try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));httpResponse = httpClient.execute(httpPost);BufferedReader br1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));String line = null;String rongyunCode=null;String msg=null;//String rongyunCode1=null;while ((line = br1.readLine()) != null) {System.out.println("line=" + line.toString());JSONObject jsonObject1 = JSONObject.fromObject(line);rongyunCode=jsonObject1.get("code").toString();if(rongyunCode.equals("200")){msg="操作成功";//踢人或退出成功后要发送群系统消息,不管此操作成不成功,不影响后续操作String types="";if (type.equals("1")){//踢出群组操作types="3";}else {types="4";}String [] addStaffId=new String[]{staffId};//操作人idStaff staff=staffMapper.selectStaffDetail(staffId);//获取员工名称String [] addStaffName=new String[]{staff.getStaffName()};groupPublish(staffId,addStaffId,addStaffName,groupId,types,groupName);//**********成功后更新头像,groupName在这里没用,充数放进去的  start//获取创建人的姓名和成员名称拼新的名称,主要作用于一开始只有两个人的情况下,新增一个组员名称会变化int res=updateRongyunGroup(groupHead,groupName,groupId);if (res<=0){return new ResultUtil<Map<String,Object>>().setAppErrorMsg("群组添加成员成功但修改头像和名称失败");}//**********成功后更新头像,groupName在这里没用,充数放进去的   end}else {msg="操作失败,失败原因为:"+codeMsg(rongyunCode);map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组idreturn new ResultUtil<Map<String,Object>>().setData(map);}}map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组id}catch (IOException e){e.printStackTrace();}return new ResultUtil<Map<String,Object>>().setData(map);}/*** 踢出群组* @param memberId  被提出人的员工id,多个用逗号隔开* @param groupId 群组id* @param groupHead  群组头像* @param groupName  群组名称* @return*/@RequestMapping(value = "/kickOutGroup",method = RequestMethod.POST)@ApiOperation(value = "踢出群组")public Result<Map<String, Object>> kickOutGroup(@RequestParam(required = true) String memberId,@RequestParam(required = true) String groupId,@RequestParam(required = false) String groupName,@RequestParam(required = true) String groupHead){//融云没有踢出群组的接口,采用了退出群组接口,由我们这边通过逻辑去实现踢人功能String type="1";//用来区分踢人和退群Result<Map<String, Object>> result=quitGroupMembers(memberId,groupId,groupName,groupHead,type);System.out.println(result);return result;}/*** 解散群组* @param staffId 操作解散群的用户id* @param groupId 要解散的群组id* @return*/@RequestMapping(value = "/dismissGroup",method = RequestMethod.POST)@ApiOperation(value = "解散群组")public Result<Map<String,Object>> dismissGroup(@RequestParam(required = true) String staffId,@RequestParam(required = true) String groupId){Map<String,Object> map=new HashMap<>();//组内成员退出的urlString url = "http://api-cn.ronghub.com/group/dismiss.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。//Logger.i(Signature);//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient = HttpClientBuilder.create().build();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("userId", staffId));//操作解散群的用户 Id(员工id)nameValuePair.add(new BasicNameValuePair("groupId", groupId));// 要解散的群 IdHttpResponse httpResponse=null;try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));httpResponse = httpClient.execute(httpPost);BufferedReader br1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));String line = null;String rongyunCode=null;String msg=null;//String rongyunCode1=null;while ((line = br1.readLine()) != null) {System.out.println("line=" + line.toString());JSONObject jsonObject1 = JSONObject.fromObject(line);rongyunCode=jsonObject1.get("code").toString();if(rongyunCode.equals("200")){msg="解散群成功";//解散成功后要发送群系统消息,不管此操作成不成功,不影响后续操作String [] addStaffId=new String[0];//该字段没用String [] addStaffName=new String[0];//该字段没用String types="5";String groupName="";//该字段没用groupPublish(staffId,addStaffId,addStaffName,groupId,types,groupName);//解散成功后要删除群组表中存储的群组数据int res=rongyunGroupService.deleteRongyunGroup(groupId);if (res<=0){return new ResultUtil<Map<String,Object>>().setAppErrorMsg("删除群组信息失败");}}else {msg="解散群失败,失败原因为:"+codeMsg(rongyunCode);map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组idreturn new ResultUtil<Map<String,Object>>().setData(map);}}map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息map.put("groupId",groupId);//群组id}catch (IOException e){e.printStackTrace();}return new ResultUtil<Map<String,Object>>().setData(map);}/*** 刷新群组信息* @param groupName  群组名称* @param groupId 群组id* @return*//*@RequestMapping(value = "/refreshGroup",method = RequestMethod.POST)@ApiOperation(value = "刷新群组信息")*/public JSONObject refreshGroup(@RequestParam(required = true) String groupId,@RequestParam(required = true) String groupName){JSONObject jsonObject=new JSONObject();//组内成员退出的urlString url = "http://api-cn.ronghub.com/group/refresh.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。//Logger.i(Signature);//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient = HttpClientBuilder.create().build();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("groupId", groupId));//群组 IdnameValuePair.add(new BasicNameValuePair("groupName", groupName));// 群组名称HttpResponse httpResponse=null;try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));httpResponse = httpClient.execute(httpPost);BufferedReader br1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));String line = null;String rongyunCode=null;String msg=null;//String rongyunCode1=null;while ((line = br1.readLine()) != null) {System.out.println("line=" + line.toString());JSONObject jsonObject1 = JSONObject.fromObject(line);rongyunCode=jsonObject1.get("code").toString();if(rongyunCode.equals("200")){msg="刷新群组信息成功";}else {msg="刷新群组信息失败,失败原因为:"+codeMsg(rongyunCode);jsonObject.put("rongyunCode",rongyunCode);//融云返回的code码jsonObject.put("rongyunMsg",msg);//融云返回的msg信息jsonObject.put("groupId",groupId);//群组idreturn jsonObject;}}jsonObject.put("rongyunCode",rongyunCode);//融云返回的code码jsonObject.put("rongyunMsg",msg);//融云返回的msg信息jsonObject.put("groupId",groupId);//群组id}catch (IOException e){e.printStackTrace();}return jsonObject;}/*** 发送群组通知消息* @param staffId  //员工id* @param addStaffId //被加入群的用户 ID* @param addStaffName //被加入群的用户 名称* @param addStaffName //主用于退出群组,如果退出的是群主则为新群主id,不是群主就为null* @param types  //0 创建群组* @param groupId  群组id* @param groupName  群组名称* @return*///@RequestMapping(value = "/groupPublish",method = RequestMethod.POST)//@ApiOperation(value = "发送群组通知消息")public Result<Map<String,Object>> groupPublish(@RequestParam(required = true) String staffId,@RequestParam(required = false) String [] addStaffId,@RequestParam(required = false) String [] addStaffName,@RequestParam(required = true) String groupId,@RequestParam(required = true) String types,@RequestParam(required = true) String groupName){Map<String,Object> map=new HashMap<>();//组内成员退出的urlString url = "http://api-cn.ronghub.com/message/group/publish.json";String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。//Logger.i(Signature);//HttpClient httpClient = new DefaultHttpClient();HttpClient httpClient = HttpClientBuilder.create().build();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("fromUserId", staffId));//发送人员工 IdnameValuePair.add(new BasicNameValuePair("toGroupId", groupId));//接收群 IdnameValuePair.add(new BasicNameValuePair("objectName", "RC:GrpNtf"));//消息类型 本类型为群组系统消息Staff staff=staffMapper.selectStaffDetail(staffId);String content="";//content逻辑开始*************JSONObject jsonObject=new JSONObject();JSONObject data=new JSONObject();if (types.equals("0")){//创建群组消息//content="{\"operatorNickname\":\""+staffId+"\",\"operation\":\"Rename\",\"data\":{\"operatorNickname\":\""+staff.getStaffName()+"\",\"targetGroupName\":\""+groupName+"\"},\"message\":\"创建群组\",\"extra\":\"\"}";jsonObject.put("operatorUserId",staffId);//操作人用户 IdjsonObject.put("operation","Create");//操作名 创建data.put("operatorNickname",staff.getStaffName());//data 数据说明:operatorNickname 为操作者data.put("targetGroupName",groupName);//data 数据说明:群组名称jsonObject.put("data",data); //datajsonObject.put("message","创建群组");jsonObject.put("extra","");content=jsonObject.toString();} else if (types.equals("1")){//修改群名称jsonObject.put("operatorUserId",staffId);//操作人用户 IdjsonObject.put("operation","Rename");//操作名 修改群名称data.put("operatorNickname",staff.getStaffName());//data 数据说明:operatorNickname 为操作者data.put("targetGroupName",groupName);//data 数据说明:群组名称jsonObject.put("data",data); //datajsonObject.put("message","修改群名称");jsonObject.put("extra","");content=jsonObject.toString();} else if (types.equals("2")){//添加群成员jsonObject.put("operatorUserId",staffId);//操作人用户 IdjsonObject.put("operation","Add");//操作名 添加群成员data.put("operatorNickname",staff.getStaffName());//data 数据说明:operatorNickname 为操作者data.put("targetUserIds",addStaffId);// 被加入群的用户 IDdata.put("targetUserDisplayNames",addStaffName);// 被加入群的用户 名称data.put("targetGroupName",groupName);//data 数据说明:群组名称jsonObject.put("data",data); //datajsonObject.put("message","添加群成员");jsonObject.put("extra","");content=jsonObject.toString();} else if (types.equals("3")){//移出群成员jsonObject.put("operatorUserId",staffId);//操作人用户 IdjsonObject.put("operation","Kicked");//操作名 移出群成员data.put("operatorNickname",staff.getStaffName());//data 数据说明:operatorNickname 为操作者data.put("targetUserIds",addStaffId);// 被移出群成员的用户 IDdata.put("targetUserDisplayNames",addStaffName);// 被移出群成员的用户 名称jsonObject.put("data",data); //datajsonObject.put("message","移出群成员");jsonObject.put("extra","");content=jsonObject.toString();}else if (types.equals("4")){//退出群组jsonObject.put("operatorUserId",staffId);//操作人用户 IdjsonObject.put("operation","Quit");//操作名 退出群组data.put("operatorNickname",staff.getStaffName());//data 数据说明:operatorNickname 为操作者data.put("targetUserIds",addStaffId);// 退出群组的用户 IDdata.put("targetUserDisplayNames",addStaffName);// 退出群组的用户 名称//data.put("newCreatorId",newCreatorId);//data 数据说明:如退出群的用户为群创建者则 newCreatorId 为新的群创建者 ID ,否则为 null(自己的业务逻辑中不允许群主退出群组)jsonObject.put("data",data); //datajsonObject.put("message","退出群组");jsonObject.put("extra","");content=jsonObject.toString();}else if (types.equals("5")){//解散群组jsonObject.put("operatorUserId",staffId);//操作人用户 IdjsonObject.put("operation","Dismiss");//操作名 解散群组data.put("operatorNickname",staff.getStaffName());//data 数据说明:operatorNickname 为操作者jsonObject.put("data",data); //datajsonObject.put("message","解散群组");jsonObject.put("extra","");content=jsonObject.toString();}//content逻辑结束*************System.out.println(content);nameValuePair.add(new BasicNameValuePair("content", content));// 发送消息内容nameValuePair.add(new BasicNameValuePair("isIncludeSender", "1"));// 发送消息内容HttpResponse httpResponse=null;try {httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));httpResponse = httpClient.execute(httpPost);BufferedReader br1 = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));String line = null;String rongyunCode=null;String msg=null;//String rongyunCode1=null;while ((line = br1.readLine()) != null) {System.out.println("line=" + line.toString());JSONObject jsonObject1 = JSONObject.fromObject(line);rongyunCode=jsonObject1.get("code").toString();if(rongyunCode.equals("200")){msg="系统消息发送成功";}else {msg="系统消息发送失败,失败原因为:"+codeMsg(rongyunCode);map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息//map.put("groupId",groupId);//群组idreturn new ResultUtil<Map<String,Object>>().setData(map);}}map.put("rongyunCode",rongyunCode);//融云返回的code码map.put("rongyunMsg",msg);//融云返回的msg信息//map.put("groupId",groupId);//群组id}catch (IOException e){e.printStackTrace();}return new ResultUtil<Map<String,Object>>().setData(map);}//SHA1加密//http://www.rongcloud.cn/docs/server.html#通用_API_接口签名规则private static String sha1(String data) {StringBuffer buf = new StringBuffer();try {MessageDigest md = MessageDigest.getInstance("SHA1");md.update(data.getBytes());byte[] bits = md.digest();for (int i = 0; i < bits.length; i++) {int a = bits[i];if (a < 0) a += 256;if (a < 16) buf.append("0");buf.append(Integer.toHexString(a));}} catch (Exception e) {}return buf.toString();}/*** 如果失败返回的各种msg信息* @param code* @return*/private static String codeMsg(String code){String msg=null;if(code.equals("400")){msg="请求参数错误";}else if(code.equals("401")){msg="未授权";}else if(code.equals("403")){msg="服务器拒绝请求";}else if(code.equals("404")){msg="服务器找不到请求的地址";}else if(code.equals("405")){msg="群容量超出上限,禁止请求此方法";}else if(code.equals("429")){msg="请求频率超限";}else if(code.equals("500")){msg="服务器内部错误";}else if(code.equals("504")){msg="网关超时";}else if(code.equals("1000")){msg="服务器端内部逻辑错误,请稍后重试";}else if(code.equals("1001")){msg="App Secret 错误";}else if(code.equals("1002")){msg="参数错误";}else if(code.equals("1003")){msg="无 POST 数据";}else if(code.equals("1004")){msg="验证签名错误";}else if(code.equals("1005")){msg="参数长度超限";}else if(code.equals("1006")){msg="App 被锁定或删除";}else if(code.equals("1007")){msg="被限制调用";}else if(code.equals("1008")){msg="调用频率超限";}else if(code.equals("1009")){msg="服务未开通";}else if(code.equals("1015")){msg="要删除的保活聊天室 ID 不存在";}else if(code.equals("1016")){msg="设置的保活聊天室个数超限";}else if(code.equals("1017")){msg="实时音视频 SDK 版本不支持";}else if(code.equals("1018")){msg="实时音视频服务未开启";}else if(code.equals("1050")){msg="内部服务响应超时";}else if(code.equals("2007")){msg="注册用户数量超限";}return msg;}
}

java获取融云token、并实现群组聊天、管理等后台接口示例相关推荐

  1. 下载telegram群组聊天消息

    下载telegram群组聊天消息及统计方案: 1 创建机器人     在telegram应用里与BotFather交互创建机器人,参考链接:https://core.telegram.org/bots ...

  2. telegram 下载群组聊天消息

    下载telegram群组聊天消息及统计方案: 1 创建机器人 在telegram应用里与BotFather交互创建机器人,参考链接:https://core.telegram.org/bots#6-b ...

  3. Zulip 2.0.3 发布,功能强大的群组聊天软件

    百度智能云 云生态狂欢季 热门云产品1折起>>>   Zulip 2.0.3 发布了,Zulip 是一个强大的开源群组聊天软件. 用 Python 编写,使用 Django 框架,支 ...

  4. 聊天源码IM聊天室模板 群组聊天app 即时通讯IM设计聊天

    (此贴长期有效) 系统提供多种方式合作,支持源码转让/支持按年整体运维合作/支持行业定制开发,更多需求请联系我们 团队Tel:15538001716 (V同) 独立IM:个性化定制,私有化部署,全功能 ...

  5. Zulip 2.0.1 发布,功能强大的群组聊天软件

    开发四年只会写业务代码,分布式高并发都不会还做程序员?   Zulip 2.0.1 发布了,Zulip 是一个强大的开源群组聊天软件. 用 Python 编写,使用 Django 框架,支持通过会话流 ...

  6. LINUX 学习笔记 账号与群组的管理

    LINUX 账号与群组的管理 UID:UserID 保存文件:/etc/passwd GID:GroupID 保存文件:/etc/group /etc/passwd 文件结构 一行代表一个账号,里面还 ...

  7. Zulip 2.0.0 发布,功能强大的群组聊天软件

    Zulip 2.0.0 已发布,Zulip 是一个强大的开源群组聊天软件. 用 Python 编写,使用 Django 框架,支持通过会话流的私人消息和群聊.Zulip 还支持快速搜索.拖放文件上传. ...

  8. 聊天服务器-解密陌生人(11)群组管理和群组聊天

    提示: 因为工程稍微有点大对我个人来说,所以可能在某些方面讲的不清楚或逻辑性不够强,如果有问题请@我. 原工程:https://github.com/LineChen/ 八.群组管理 客户端可以发起多 ...

  9. Android 融云单聊与群聊消息免打扰功能设置与消息删除功能实现

    一.设置群聊与单聊消息免打扰功能: 1.下面直接进入逻辑代码: 实现监听事件: /*** 设置会话列表界面操作的监听器.*/RongIM.setConversationListBehaviorList ...

  10. 融云及时通讯 加入群聊

    //跳转到融云群聊天界面 RongIM.getInstance().startConversation(AddGroupChatActivity.this, Conversation.Conversa ...

最新文章

  1. springboot-异常处理使用与原理解析
  2. MULE ESB中的一些值得关注的地方
  3. [ext/iconv/iconv.lo] Error 1
  4. 20100519 学习记录:asp CreateFolder/上传附件
  5. [译]看漫画学Flux
  6. 近一半程序员单身、年薪低于 15 万,程序员扎心现状大调查!
  7. EOS开发dApp前需要了解的五件事
  8. 华为平板鸿蒙发布,华为将发布鸿蒙平板,你期待吗?
  9. sqlserver express版PRIMARY 大小不能超过4G
  10. ubuntu播放文件需要MPEG-4 AAC解码器
  11. Mac下GitHub安装及使用教程
  12. BIM应用落地:基于BIM的群塔作业方案优化
  13. Nr,GenBank, RefSeq, UniProt 数据库的异同
  14. 二分查找、分治算法——汉诺塔问题
  15. win11关闭开盖开机 / Windows11关闭掀盖自动开机
  16. 判断iOS6/iOS7, 3.5inch/4.0inch
  17. jmeter逻辑控制器之while循环控制器(一)
  18. 《PBI系列 快选 人气新品池 品质档 03》花随花心著
  19. 计算机科学与技术考研预报名,考研预报名有哪些注意事项
  20. 设计可复用的OO软件

热门文章

  1. android 九宫格图片工具,Android 图片选择、预览、九宫格图片控件、拖拽排序九宫格图片控件...
  2. 有一个3×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及其所在的行号和列号
  3. Java8 新特性之stream
  4. 学计算机土味情话,很套路的土味情话
  5. 微信小程序-Testerhome
  6. 一口气说出 OAuth2.0 的四种授权方式,面试官会高看一眼
  7. 《精进:如何成为一个很厉害的人》 采铜
  8. java常用的编辑器之kindeditor
  9. 企业微信小程序-获取员工信息
  10. HTTP和AJAX重点知识