|
|
@@ -19,434 +19,475 @@ import java.util.Map.Entry;
|
|
|
/**
|
|
|
* 网易通讯接口
|
|
|
* 只对接网易通讯
|
|
|
+ *
|
|
|
* @author 刘迎奥
|
|
|
- * @date 2018年7月5日
|
|
|
* @version v1.0
|
|
|
+ * @date 2018年7月5日
|
|
|
*/
|
|
|
public class Nimserver {
|
|
|
-
|
|
|
- DefaultHttpClient httpClient = new DefaultHttpClient();
|
|
|
- private String appKey = NimConfig.appKey;//开发者平台分配的appkey
|
|
|
- private String appSecret = NimConfig.appSecret;
|
|
|
-
|
|
|
- public HttpPost init(String url, String contentType){
|
|
|
-
|
|
|
- String nonce = (int)(Math.random() * 1000000)+"";//随机数(最大长度128个字符)
|
|
|
- String curTime = String.valueOf((new Date()).getTime() / 1000L);
|
|
|
- String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码
|
|
|
-
|
|
|
- HttpPost httpPost = new HttpPost(url);
|
|
|
- // 设置请求的header
|
|
|
- httpPost.addHeader("AppKey", appKey);
|
|
|
- httpPost.addHeader("Nonce", nonce);
|
|
|
- httpPost.addHeader("CurTime", curTime);
|
|
|
- httpPost.addHeader("CheckSum", checkSum);
|
|
|
- httpPost.addHeader("Content-Type", contentType);
|
|
|
- return httpPost;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 注册
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String createUser(String accid, String name){
|
|
|
- try {
|
|
|
- String url = "https://api.netease.im/nimserver/user/create.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- nvps.add(new BasicNameValuePair("name", name == null ? "" : name));
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户名片
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String getUinfos(String[] accids){
|
|
|
- try {
|
|
|
-
|
|
|
- String url = "https://api.netease.im/nimserver/user/getUinfos.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accids", JSON.toJSONString(accids)));
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- //{"code":200,"uinfos":[{"accid":"liuyingao","gender":0}]}
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新用户 token
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String refreshToken(String accid){
|
|
|
- try {
|
|
|
-
|
|
|
- String url = "https://api.netease.im/nimserver/user/refreshToken.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 网易云通信ID更新
|
|
|
- * 可以修改指定的 token
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String update(String accid, String token, String props){
|
|
|
- try {
|
|
|
-
|
|
|
- String url = "https://api.netease.im/nimserver/user/update.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
-
|
|
|
- if(token != null && !token.equals("")) {
|
|
|
- nvps.add(new BasicNameValuePair("token", token));
|
|
|
- }
|
|
|
-
|
|
|
- //json属性,第三方可选填,最大长度1024字符
|
|
|
- if(props != null && !props.equals("")) {
|
|
|
- nvps.add(new BasicNameValuePair("props", props));
|
|
|
- }
|
|
|
-
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新用户名片
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String updateUinfo(String accid, Map<String, String> paramMap){
|
|
|
- try {
|
|
|
-
|
|
|
- String url = "https://api.netease.im/nimserver/user/updateUinfo.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- if(paramMap!=null) {
|
|
|
- for (Entry<String, String> item : paramMap.entrySet()) {
|
|
|
- String key = item.getKey();
|
|
|
- String val = item.getValue();
|
|
|
- nvps.add(new BasicNameValuePair(key, val));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
- /**
|
|
|
- * 设置桌面端在线时,移动端是否需要推送
|
|
|
- * @param accid
|
|
|
- * @param donnopOpen
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String setDonnop(String accid, String donnopOpen){
|
|
|
- try {
|
|
|
-
|
|
|
- String url = "https://api.netease.im/nimserver/user/setDonnop.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- nvps.add(new BasicNameValuePair("donnopOpen", donnopOpen));
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 加好友
|
|
|
- * @param accid 加好友发起者accid
|
|
|
- * @param faccid 加好友接收者accid
|
|
|
- * @param type 1直接加好友,2请求加好友,3同意加好友,4拒绝加好友
|
|
|
- * @param msg 加好友对应的请求消息,第三方组装,最长256字符(不是必须参数)
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String addFriend(String accid, String faccid, String type, String msg){
|
|
|
- try {
|
|
|
- String url = "https://api.netease.im/nimserver/friend/add.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
- nvps.add(new BasicNameValuePair("type", type));
|
|
|
-
|
|
|
- if(msg!=null && !msg.equals("")){
|
|
|
- nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
- }
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 更新好友相关信息
|
|
|
- * @param accid 发起者accid
|
|
|
- * @param faccid 要修改朋友的accid
|
|
|
- * @param alias 给好友增加备注名,限制长度128(不是必须参数)
|
|
|
- * @param ex 修改ex字段,限制长度256(不是必须参数)
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String updateFriend(String accid, String faccid, String alias, String ex){
|
|
|
- try {
|
|
|
- String url = "https://api.netease.im/nimserver/friend/update.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
-
|
|
|
- if(alias!=null && !alias.equals("")){
|
|
|
- nvps.add(new BasicNameValuePair("alias", alias));
|
|
|
- }
|
|
|
- if(ex!=null && !ex.equals("")){
|
|
|
- nvps.add(new BasicNameValuePair("ex", ex));
|
|
|
- }
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除好友
|
|
|
- * @param accid 发起者accid
|
|
|
- * @param faccid 要删除朋友的accid
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String deleteFriend(String accid, String faccid){
|
|
|
- try {
|
|
|
- String url = "https://api.netease.im/nimserver/friend/delete.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
- nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
-
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送普通消息
|
|
|
- * @param from 发送者accid,用户帐号,最大32字符,必须保证一个APP内唯一
|
|
|
- * @param ope 0:点对点个人消息,1:群消息(高级群),其他返回414
|
|
|
- * @param to ope==0是表示accid即用户id,ope==1表示tid即群id
|
|
|
- * @param type 0 表示文本消息<br>
|
|
|
- 1 表示图片<br>
|
|
|
- 2 表示语音<br>
|
|
|
- 3 表示视频<br>
|
|
|
- 4 表示地理位置信息<br>
|
|
|
- 6 表示文件<br>
|
|
|
- 100 自定义消息类型(特别注意,对于未对接易盾反垃圾功能的应用,该类型的消息不会提交反垃圾系统检测)<br>
|
|
|
- * @param body 请参考下方消息示例说明中对应消息的body字段,最大长度5000字符,为一个JSON串
|
|
|
- * @param paramMap
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String sendMsg(String from, String ope, String to, String type, String body, Map<String, String> paramMap){
|
|
|
- try {
|
|
|
- String url = "https://api.netease.im/nimserver/msg/sendMsg.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("from", from));
|
|
|
- nvps.add(new BasicNameValuePair("ope", ope));
|
|
|
- nvps.add(new BasicNameValuePair("to", to));
|
|
|
- nvps.add(new BasicNameValuePair("type", type));
|
|
|
- nvps.add(new BasicNameValuePair("body", body));
|
|
|
-
|
|
|
- for (Entry<String, String> item : paramMap.entrySet()) {
|
|
|
- String key = item.getKey();
|
|
|
- String val = item.getValue();
|
|
|
- nvps.add(new BasicNameValuePair(key, val));
|
|
|
- }
|
|
|
-
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发送自定义系统通知
|
|
|
- * @param from 发送者accid,用户帐号,最大32字符,APP内唯一
|
|
|
- * @param msgtype 0:点对点自定义通知,1:群消息自定义通知,其他返回414
|
|
|
- * @param to msgtype==0是表示accid即用户id,msgtype==1表示tid即群id
|
|
|
- * @param attach 自定义通知内容,第三方组装的字符串,建议是JSON串,最大长度4096字符
|
|
|
- * @param paramMap
|
|
|
- * pushcontent String 否 iOS推送内容,第三方自己组装的推送内容,不超过150字符
|
|
|
- * payload String 否 iOS推送对应的payload,必须是JSON,不能超过2k字符
|
|
|
- * sound String 否 如果有指定推送,此属性指定为客户端本地的声音文件名,长度不要超过30个字符,如果不指定,会使用默认声音
|
|
|
- * save int 否 1表示只发在线,2表示会存离线,其他会报414错误。默认会存离线
|
|
|
- * option String 否
|
|
|
- * 发消息时特殊指定的行为选项,Json格式,可用于指定消息计数等特殊行为;option中字段不填时表示默认值。
|
|
|
- * option示例:
|
|
|
- * {"badge":false,"needPushNick":false,"route":false}
|
|
|
- * 字段说明:
|
|
|
- * 1. badge:该消息是否需要计入到未读计数中,默认true;
|
|
|
- * 2. needPushNick: 推送文案是否需要带上昵称,不设置该参数时默认false(ps:注意与sendMsg.action接口有别);
|
|
|
- * 3. route: 该消息是否需要抄送第三方;默认true (需要app开通消息抄送功能)
|
|
|
- * @return
|
|
|
- */
|
|
|
- public String sendAttachMsg(String from, int msgtype, String to, String attach, Map<String, String> paramMap) {
|
|
|
- try {
|
|
|
- String url = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
|
|
|
- //初始化
|
|
|
- HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
-
|
|
|
- // 设置请求的参数
|
|
|
- List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
- nvps.add(new BasicNameValuePair("from", from));
|
|
|
- nvps.add(new BasicNameValuePair("msgtype", msgtype+""));
|
|
|
- nvps.add(new BasicNameValuePair("to", to));
|
|
|
- nvps.add(new BasicNameValuePair("attach", attach));
|
|
|
-
|
|
|
- for (Entry<String, String> item : paramMap.entrySet()) {
|
|
|
- String key = item.getKey();
|
|
|
- String val = item.getValue();
|
|
|
- nvps.add(new BasicNameValuePair(key, val));
|
|
|
- }
|
|
|
-
|
|
|
- httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
-
|
|
|
- // 执行请求
|
|
|
- HttpResponse response = httpClient.execute(httpPost);
|
|
|
-
|
|
|
- // 打印执行结果
|
|
|
- String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
- return result;
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
- return NimConfig.errorNeteaseMessage;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ DefaultHttpClient httpClient = new DefaultHttpClient();
|
|
|
+ private String appKey = NimConfig.appKey;//开发者平台分配的appkey
|
|
|
+ private String appSecret = NimConfig.appSecret;
|
|
|
+
|
|
|
+ public HttpPost init(String url, String contentType) {
|
|
|
+
|
|
|
+ String nonce = (int) (Math.random() * 1000000) + "";//随机数(最大长度128个字符)
|
|
|
+ String curTime = String.valueOf((new Date()).getTime() / 1000L);
|
|
|
+ String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ // 设置请求的header
|
|
|
+ httpPost.addHeader("AppKey", appKey);
|
|
|
+ httpPost.addHeader("Nonce", nonce);
|
|
|
+ httpPost.addHeader("CurTime", curTime);
|
|
|
+ httpPost.addHeader("CheckSum", checkSum);
|
|
|
+ httpPost.addHeader("Content-Type", contentType);
|
|
|
+ return httpPost;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String createUser(String accid, String name) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/user/create.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ nvps.add(new BasicNameValuePair("name", name == null ? "" : name));
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户名片
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getUinfos(String[] accids) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ String url = "https://api.netease.im/nimserver/user/getUinfos.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accids", JSON.toJSONString(accids)));
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ //{"code":200,"uinfos":[{"accid":"liuyingao","gender":0}]}
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户 token
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String refreshToken(String accid) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ String url = "https://api.netease.im/nimserver/user/refreshToken.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 网易云通信ID更新
|
|
|
+ * 可以修改指定的 token
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String update(String accid, String token, String props) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ String url = "https://api.netease.im/nimserver/user/update.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+
|
|
|
+ if (token != null && !token.equals("")) {
|
|
|
+ nvps.add(new BasicNameValuePair("token", token));
|
|
|
+ }
|
|
|
+
|
|
|
+ //json属性,第三方可选填,最大长度1024字符
|
|
|
+ if (props != null && !props.equals("")) {
|
|
|
+ nvps.add(new BasicNameValuePair("props", props));
|
|
|
+ }
|
|
|
+
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户名片
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String updateUinfo(String accid, Map<String, String> paramMap) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ String url = "https://api.netease.im/nimserver/user/updateUinfo.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ if (paramMap != null) {
|
|
|
+ for (Entry<String, String> item : paramMap.entrySet()) {
|
|
|
+ String key = item.getKey();
|
|
|
+ String val = item.getValue();
|
|
|
+ nvps.add(new BasicNameValuePair(key, val));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置桌面端在线时,移动端是否需要推送
|
|
|
+ *
|
|
|
+ * @param accid
|
|
|
+ * @param donnopOpen
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String setDonnop(String accid, String donnopOpen) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ String url = "https://api.netease.im/nimserver/user/setDonnop.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ nvps.add(new BasicNameValuePair("donnopOpen", donnopOpen));
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 加好友
|
|
|
+ *
|
|
|
+ * @param accid 加好友发起者accid
|
|
|
+ * @param faccid 加好友接收者accid
|
|
|
+ * @param type 1直接加好友,2请求加好友,3同意加好友,4拒绝加好友
|
|
|
+ * @param msg 加好友对应的请求消息,第三方组装,最长256字符(不是必须参数)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String addFriend(String accid, String faccid, String type, String msg) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/friend/add.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
+ nvps.add(new BasicNameValuePair("type", type));
|
|
|
+
|
|
|
+ if (msg != null && !msg.equals("")) {
|
|
|
+ nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
+ }
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新好友相关信息
|
|
|
+ *
|
|
|
+ * @param accid 发起者accid
|
|
|
+ * @param faccid 要修改朋友的accid
|
|
|
+ * @param alias 给好友增加备注名,限制长度128(不是必须参数)
|
|
|
+ * @param ex 修改ex字段,限制长度256(不是必须参数)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String updateFriend(String accid, String faccid, String alias, String ex) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/friend/update.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
+
|
|
|
+ if (alias != null && !alias.equals("")) {
|
|
|
+ nvps.add(new BasicNameValuePair("alias", alias));
|
|
|
+ }
|
|
|
+ if (ex != null && !ex.equals("")) {
|
|
|
+ nvps.add(new BasicNameValuePair("ex", ex));
|
|
|
+ }
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除好友
|
|
|
+ *
|
|
|
+ * @param accid 发起者accid
|
|
|
+ * @param faccid 要删除朋友的accid
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String deleteFriend(String accid, String faccid) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/friend/delete.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("accid", accid));
|
|
|
+ nvps.add(new BasicNameValuePair("faccid", faccid));
|
|
|
+
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送普通消息
|
|
|
+ *
|
|
|
+ * @param from 发送者accid,用户帐号,最大32字符,必须保证一个APP内唯一
|
|
|
+ * @param ope 0:点对点个人消息,1:群消息(高级群),其他返回414
|
|
|
+ * @param to ope==0是表示accid即用户id,ope==1表示tid即群id
|
|
|
+ * @param type 0 表示文本消息<br>
|
|
|
+ * 1 表示图片<br>
|
|
|
+ * 2 表示语音<br>
|
|
|
+ * 3 表示视频<br>
|
|
|
+ * 4 表示地理位置信息<br>
|
|
|
+ * 6 表示文件<br>
|
|
|
+ * 100 自定义消息类型(特别注意,对于未对接易盾反垃圾功能的应用,该类型的消息不会提交反垃圾系统检测)<br>
|
|
|
+ * @param body 请参考下方消息示例说明中对应消息的body字段,最大长度5000字符,为一个JSON串
|
|
|
+ * @param paramMap
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String sendMsg(String from, String ope, String to, String type, String body, Map<String, String> paramMap) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/msg/sendMsg.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("from", from));
|
|
|
+ nvps.add(new BasicNameValuePair("ope", ope));
|
|
|
+ nvps.add(new BasicNameValuePair("to", to));
|
|
|
+ nvps.add(new BasicNameValuePair("type", type));
|
|
|
+ nvps.add(new BasicNameValuePair("body", body));
|
|
|
+
|
|
|
+ for (Entry<String, String> item : paramMap.entrySet()) {
|
|
|
+ String key = item.getKey();
|
|
|
+ String val = item.getValue();
|
|
|
+ nvps.add(new BasicNameValuePair(key, val));
|
|
|
+ }
|
|
|
+
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送自定义系统通知
|
|
|
+ *
|
|
|
+ * @param from 发送者accid,用户帐号,最大32字符,APP内唯一
|
|
|
+ * @param msgtype 0:点对点自定义通知,1:群消息自定义通知,其他返回414
|
|
|
+ * @param to msgtype==0是表示accid即用户id,msgtype==1表示tid即群id
|
|
|
+ * @param attach 自定义通知内容,第三方组装的字符串,建议是JSON串,最大长度4096字符
|
|
|
+ * @param paramMap pushcontent String 否 iOS推送内容,第三方自己组装的推送内容,不超过150字符
|
|
|
+ * payload String 否 iOS推送对应的payload,必须是JSON,不能超过2k字符
|
|
|
+ * sound String 否 如果有指定推送,此属性指定为客户端本地的声音文件名,长度不要超过30个字符,如果不指定,会使用默认声音
|
|
|
+ * save int 否 1表示只发在线,2表示会存离线,其他会报414错误。默认会存离线
|
|
|
+ * option String 否
|
|
|
+ * 发消息时特殊指定的行为选项,Json格式,可用于指定消息计数等特殊行为;option中字段不填时表示默认值。
|
|
|
+ * option示例:
|
|
|
+ * {"badge":false,"needPushNick":false,"route":false}
|
|
|
+ * 字段说明:
|
|
|
+ * 1. badge:该消息是否需要计入到未读计数中,默认true;
|
|
|
+ * 2. needPushNick: 推送文案是否需要带上昵称,不设置该参数时默认false(ps:注意与sendMsg.action接口有别);
|
|
|
+ * 3. route: 该消息是否需要抄送第三方;默认true (需要app开通消息抄送功能)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String sendAttachMsg(String from, int msgtype, String to, String attach, Map<String, String> paramMap) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("from", from));
|
|
|
+ nvps.add(new BasicNameValuePair("msgtype", msgtype + ""));
|
|
|
+ nvps.add(new BasicNameValuePair("to", to));
|
|
|
+ nvps.add(new BasicNameValuePair("attach", attach));
|
|
|
+
|
|
|
+ for (Entry<String, String> item : paramMap.entrySet()) {
|
|
|
+ String key = item.getKey();
|
|
|
+ String val = item.getValue();
|
|
|
+ nvps.add(new BasicNameValuePair(key, val));
|
|
|
+ }
|
|
|
+
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String history(String from, String to, String begintime, String endtime) {
|
|
|
+ try {
|
|
|
+ String url = "https://api.netease.im/nimserver/history/querySessionMsg.action";
|
|
|
+ //初始化
|
|
|
+ HttpPost httpPost = init(url, NimConfig.contentType_1);
|
|
|
+
|
|
|
+ // 设置请求的参数
|
|
|
+ List<NameValuePair> nvps = new ArrayList<NameValuePair>();
|
|
|
+ nvps.add(new BasicNameValuePair("from", from));
|
|
|
+ nvps.add(new BasicNameValuePair("to", to));
|
|
|
+ nvps.add(new BasicNameValuePair("begintime", begintime));
|
|
|
+ nvps.add(new BasicNameValuePair("endtime", endtime));
|
|
|
+ nvps.add(new BasicNameValuePair("limit", "100"));
|
|
|
+
|
|
|
+
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
|
|
|
+
|
|
|
+ // 执行请求
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+ // 打印执行结果
|
|
|
+ String result = EntityUtils.toString(response.getEntity(), "utf-8");
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ return NimConfig.errorNeteaseMessage;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Nimserver nimserver = new Nimserver();
|
|
|
+ System.out.println(nimserver.history("269", "1391", "1546013440000", "1546423840000"));;
|
|
|
+ }
|
|
|
+
|
|
|
}
|