网易云信(创建账号,添加好友,获取好友关系,发送系统消息《推送》,删除好友,修改用户信息)
//创建网易云通信ID
public Netease getNeteaseToken(UserPatient userPraientNew) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/user/create.action";
HttpPost httpPost = new HttpPost(url);
String accid = neteaseConfig.getAppKey();
String appSecret = neteaseConfig.getAppSecret();
String nonce = neteaseConfig.getNonce();
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", accid);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", DateUtils.formatDateToNumberStr(new Date())));
nvps.add(new BasicNameValuePair("name", userPraientNew.getUserName()));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
String json = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject str = StringUtils.jsonToStrName(json, "info");
Netease netease = new Netease();
netease.setAccid(str.getString("accid"));
netease.setName(str.getString("name"));
netease.setToken(str.getString("token"));
// 打印执行结果
return netease;
}
//获取好友关系
public List<DoctorBean> getAction(String accid, Long updateTime) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/friend/get.action";
HttpPost httpPost = new HttpPost(url);
String appKey = neteaseConfig.getAppKey();
String appSecret = neteaseConfig.getAppSecret();
String nonce = neteaseConfig.getNonce();
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", appKey);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", accid));
nvps.add(new BasicNameValuePair("updatetime", String.valueOf(updateTime)));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
String json = EntityUtils.toString(response.getEntity(), "utf-8");
ResultBean resultBean = (ResultBean) JSONToObject(json, ResultBean.class);
// 打印执行结果
return resultBean.getFriends();
}
//添加好友
public String add(String accid, String fAccid) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/friend/add.action";
HttpPost httpPost = new HttpPost(url);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String appKey = neteaseConfig.getAppKey();
String appSecret = neteaseConfig.getAppSecret();
String nonce = neteaseConfig.getNonce();
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", appKey);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", accid));
nvps.add(new BasicNameValuePair("faccid", fAccid));
// type = 1 直接添加成功
nvps.add(new BasicNameValuePair("type", "1"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
String json = EntityUtils.toString(response.getEntity(), "utf-8");
return json;
}
/**
*修改好友信息(备注)
* 未完善
*/
public String update() throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/friend/update.action";
HttpPost httpPost = new HttpPost(url);
String accid = neteaseConfig.getAppKey();
String appSecret = neteaseConfig.getAppSecret();
String nonce = neteaseConfig.getNonce();
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", accid);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", "25105112"));
nvps.add(new BasicNameValuePair("faccid", "27154329"));
nvps.add(new BasicNameValuePair("alias", "ZERO"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
String json = EntityUtils.toString(response.getEntity(), "utf-8");
return json;
}
/**
*修改用户信息
*/
public String updateUinfo(UserPatient userPatient) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/user/updateUinfo.action";
HttpPost httpPost = new HttpPost(url);
String accid = neteaseConfig.getAppKey();
String appSecret = neteaseConfig.getAppSecret();
String nonce = neteaseConfig.getNonce();
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", accid);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", userPatient.getNeteaseAccid()));
nvps.add(new BasicNameValuePair("name", userPatient.getUserName()));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
String json = EntityUtils.toString(response.getEntity(), "utf-8");
return json;
}
/**
*删除好友
*/
public String del(String accid, String fAccid) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/friend/delete.action";
HttpPost httpPost = new HttpPost(url);
String appKey = neteaseConfig.getAppKey();
String appSecret = neteaseConfig.getAppSecret();
String nonce = neteaseConfig.getNonce();
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("AppKey", appKey);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的参数
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 json = EntityUtils.toString(response.getEntity(), "utf-8");
return json;
}
/**
*发送系统消息
*/
public String sendMsg(String accid, String faccid, String jsonStr) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
HttpPost httpPost = new HttpPost(url);
String appKey = neteaseConfig.getAppKey();
String nonce = neteaseConfig.getNonce();
String appSecret = neteaseConfig.getAppSecret();
;
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
// 设置请求的header
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("AppKey", appKey);
// 设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("accid", accid));
nvps.add(new BasicNameValuePair("msgtype", "0"));
nvps.add(new BasicNameValuePair("to", faccid));
nvps.add(new BasicNameValuePair("body", jsonStr));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
// 打印执行结果
String json = EntityUtils.toString(response.getEntity(), "utf-8");
return json;
}
更多精彩

