quartzPro/.svn/pristine/67/671861cd430b74a11fb2bcc6b6d...

245 lines
8.9 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.ifish.netease;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ifish.enums.NeteaseEnum;
import com.ifish.util.IfishUtil;
public class NeteaseIM {
String appKey = "";
String appSecret = "";
private static Logger log = LoggerFactory.getLogger(NeteaseIM.class);
public NeteaseIM(String appKey,String appSecret){
this.appKey=appKey;
this.appSecret=appSecret;
}
/**
* 创建云信ID
* @param accid 云信ID最大长度32字符必须保证一个APP内唯一
* @param name 云信ID昵称最大长度64字符用来PUSH推送 时显示的昵称
* @param icon 云信ID头像URL第三方可选填最大长度1024
* @param props json属性第三方可选填最大长度1024字符
* @return
* @throws Exception
*/
public Map<String, String> createAccid(String accid,String name,String icon,String props){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/user/create.action";
HttpPost httpPost = new HttpPost(url);
String nonce = IfishUtil.getCharAndNumr(10);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
//计算CheckSum
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
//设置请求的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("name", name));
nvps.add(new BasicNameValuePair("icon", icon));
nvps.add(new BasicNameValuePair("props", props));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//执行请求
HttpResponse response = httpClient.execute(httpPost);
//执行结果
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject json = new JSONObject(responseStr);
String code = json.getString("code");
Map<String, String> map = new HashMap<String, String>();
map.put("code", code);
//200
if(code.equals(NeteaseEnum.status200.getKey())){
JSONObject infoJson = json.getJSONObject("info");
String token = infoJson.getString("token");
map.put("token", token);
}
else if(code.equals(NeteaseEnum.status414.getKey())){
String desc = json.getString("desc");
}
return map;
} catch (Exception e) {
log.error("CreateAccid error message:{},{}",accid,e.toString());
}
return null;
}
/**
* 更新并获取新token
* @param accid
* @return
* @throws Exception
*/
public Map<String, String> refreshToken(String accid){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/user/refreshToken.action";
HttpPost httpPost = new HttpPost(url);
String nonce = IfishUtil.getCharAndNumr(10);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
//计算CheckSum
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
//设置请求的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));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//执行请求
HttpResponse response = httpClient.execute(httpPost);
//执行结果
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject json = new JSONObject(responseStr);
String code = json.getString("code");
Map<String, String> map = new HashMap<String, String>();
map.put("code", code);
//200
if(code.equals(NeteaseEnum.status200.getKey())){
JSONObject infoJson = json.getJSONObject("info");
String token = infoJson.getString("token");
map.put("token", token);
}
else if(code.equals(NeteaseEnum.status414.getKey())){
String desc = json.getString("desc");
}
return map;
} catch (Exception e) {
log.error("refreshToken error message:{},{}",accid,e.toString());
}
return null;
}
/**
* 发送普通消息
* @param from
* @param to
* @param msg
* @return
*/
public Map<String, String> sendMsg(String from,String to,String msg){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/msg/sendMsg.action";
HttpPost httpPost = new HttpPost(url);
String nonce = IfishUtil.getCharAndNumr(10);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
//计算CheckSum
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
//设置请求的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("from", from));
nvps.add(new BasicNameValuePair("to", to));
nvps.add(new BasicNameValuePair("ope", "0"));
nvps.add(new BasicNameValuePair("type", "0"));
nvps.add(new BasicNameValuePair("body", "{\"msg\":\""+msg+"\"}"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//执行请求
HttpResponse response = httpClient.execute(httpPost);
//执行结果
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject json = new JSONObject(responseStr);
String code = json.getString("code");
Map<String, String> map = new HashMap<String, String>();
map.put("code", code);
//200
if(code.equals(NeteaseEnum.status200.getKey())){
}
else if(code.equals(NeteaseEnum.status414.getKey())){
String desc = json.getString("desc");
}
return map;
} catch (Exception e) {
log.error("refreshToken error message:{}",e.toString());
}
return null;
}
/**
* 批量发送点对点普通消息
* @param fromAccid
* @param toAccids
* @param msg
* @return
*/
public Map<String, String> sendBatchMsg(String fromAccid,String toAccids,String msg){
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/msg/sendBatchMsg.action";
HttpPost httpPost = new HttpPost(url);
String nonce = IfishUtil.getCharAndNumr(10);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
//计算CheckSum
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);
//设置请求的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("fromAccid", fromAccid));
nvps.add(new BasicNameValuePair("toAccids", toAccids));
nvps.add(new BasicNameValuePair("type", "0"));
nvps.add(new BasicNameValuePair("body", "{\"msg\":\""+msg+"\"}"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//执行请求
HttpResponse response = httpClient.execute(httpPost);
//执行结果
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject json = new JSONObject(responseStr);
String code = json.getString("code");
Map<String, String> map = new HashMap<String, String>();
map.put("code", code);
//200
if(code.equals(NeteaseEnum.status200.getKey())){
}
else if(code.equals(NeteaseEnum.status414.getKey())){
String desc = json.getString("desc");
}
return map;
} catch (Exception e) {
log.error("refreshToken error message:{}",e.toString());
}
return null;
}
public static void main(String[] args) {
List<String> list= new ArrayList<String>();
list.add("hello");
list.add("hello1");
new NeteaseIM("87b0e3315dfc2df08060bcb54246da68", "e62f6c247b46").createAccid("ifish", "爱鱼奇", "", "");
}
}