1、https://www.easemob.com/ 环信官网注册账号
2、创建应用
3、创建完成之后点击
会出现如下信息,其中标注的为重要信息
4、编写环信工具类HuanXinUtil
package com.fh.util;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import net.sf.json.JSONObject;
public class HuanXinUtil {
// private String targetURL = "https://a1.easemob.com/1177180507177107/bit/chatrooms";
private String uri = "https://a1.easemob.com/1177180507177107/bit/";
private String hx_client_id = "";
private String hx_client_Secret = "";
private String hx_access_token = "";
private long hx_token_time = 0;
private static String profilepath = "WEB-INF/classes/hx_key.properties";
public HuanXinUtil(){
String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"../../"; //项目路径
filePath = filePath.replaceAll("file:/", "");
filePath = filePath.replaceAll("%20", " ");
filePath = filePath.trim()+profilepath;
//filePath = "D://hx_key.properties";
Properties properties = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
properties.load(in);
hx_client_id = properties.getProperty("hx_client_id");
hx_client_Secret = properties.getProperty("hx_client_Secret");
hx_access_token = properties.getProperty("hx_access_token");
hx_token_time = Long.parseLong(properties.getProperty("hx_token_time"));
in.close();
Date date = new Date(hx_token_time);
System.out.println(date.before(new Date()));
if(date.before(new Date())){
hx_access_token=this.getAccessToken(hx_client_id, hx_client_Secret);
Calendar now = Calendar.getInstance();
now.setTime(new Date());
now.set(Calendar.DATE, now.get(Calendar.DATE) + 5);
hx_token_time =now.getTimeInMillis();
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
properties.setProperty("hx_token_time", Long.toString(hx_token_time));
properties.setProperty("hx_access_token", hx_access_token);
properties.store(fileOutputStream, "update access_token");
fileOutputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch