java 实现对环信账号的增删改查

简介:

package com.shangyu.huanxin;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

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.ClientProtocolException;

import org.apache.http.client.methods.HttpDelete;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

import org.json.JSONObject;


import com.shangyu.utils.JsonUtils;


public class SysUserHuanXinService {

private static String tokenurl = "https://a1.easemob.com/97501752/officeautosystem/token";

private static String userurl = "https://a1.easemob.com/97501752/officeautomationsystem/users";

private static String client_id = "YXA65UxWkI34Eea-M5V9ogdgQ";

private static String client_secret = "YXA6ahOOOn95cT-LC0_jvc5qisqqLKU";

private static String grant_type = "client_credentials";

private static String charset = "UTF-8";

/**

* @author hekang

* @return strtoken

* @throws ClientProtocolException

* @throws IOException

*/

public static String getToken() throws ClientProtocolException, IOException{

Map<String,String> createMap = new HashMap<String,String>();

        createMap.put("client_id", client_id);  

        createMap.put("client_secret", client_secret);  

        createMap.put("grant_type", grant_type); 

        HttpPost httpPost = new HttpPost(tokenurl); 

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();  

        nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

        for(NameValuePair nameValuePair:nvps){

        httpPost.addHeader(nameValuePair.getName(), nameValuePair.getValue());

        }

        try {

httpPost.setEntity(new StringEntity(JsonUtils.pojo2json(createMap), charset));

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

System.out.println(e.getMessage());

}

        HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

        String retSrc = EntityUtils.toString(httpResponse.getEntity());

        JSONObject obj = new JSONObject(retSrc); 

        String strtoken = obj.get("access_token").toString();

return strtoken;

}

/**

* addHuanXinUser

* @author hekang

* @param uid

* @param PlainPassword

* @param realName

*/

public void addHuanXinUser(String uid, String PlainPassword, String realName){

        Map<String,String> params = new HashMap<String,String>();

        params.put("username", uid);  

        params.put("password", PlainPassword);  

        params.put("nickname", realName); 

        HttpPost httpPost = new HttpPost(userurl);

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();  

        nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

        String token;

try {

token = getToken();

System.out.println("---token----"+token);

nvps.add(new BasicNameValuePair("Authorization", "Bearer "+token));

} catch (ClientProtocolException e) {

e.printStackTrace();

System.out.println("--ClientProtocolException--"+e.getMessage());

} catch (IOException e) {

e.printStackTrace();

System.out.println("--IOException--"+e.getMessage());

}

for(NameValuePair nameValuePair:nvps){

        httpPost.addHeader(nameValuePair.getName(), nameValuePair.getValue());

        }

        try {

        httpPost.setEntity(new StringEntity(JsonUtils.pojo2json(params),"utf-8"));

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

        

        try {

HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);

if(httpResponse != null){

int statusCode = httpResponse.getStatusLine().getStatusCode();

if(statusCode == 200){

System.out.println("--add--");

}

}

String retSrc = EntityUtils.toString(httpResponse.getEntity());

System.out.println("---success---"+retSrc);

/*JSONObject json = new JSONObject(retSrc);

   if("duplicate_unique_property_exists".equals(json.get("error").toString())){

   System.out.println("---该用户已经存在---不允许再次添加-");

   }*/

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

        System.out.println("-----add---end---success---");

}

/**

* deleteByuid

* @author hekang

* @param uid

*/

public void deleteByuid(String uid){

String token;

HttpResponse httpResponse;


HttpDelete httpdelete = new HttpDelete(userurl+"/"+uid);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();  

        nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

try {

token = getToken();

nvps.add(new BasicNameValuePair("Authorization", "Bearer "+token));

} catch (ClientProtocolException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

        for(NameValuePair nameValuePair:nvps){

        httpdelete.addHeader(nameValuePair.getName(), nameValuePair.getValue());

        }

        try {

httpResponse = new DefaultHttpClient().execute(httpdelete);

if(httpResponse != null){

int statusCode = httpResponse.getStatusLine().getStatusCode();

if(statusCode == 200){

System.out.println("--del--");

}

}

String retSrc = EntityUtils.toString(httpResponse.getEntity());

System.out.println("----success----"+retSrc);

/*JSONObject json = new JSONObject(retSrc);

if("service_resource_not_found".equals(json.get("error").toString())){

System.out.println("--环信服务器不存在该用户--");

}*/

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

        System.out.println("---del---end---success---");

}

/**

* put

* @author hekang

* @param uid

*/

public void putHuanXinUser(String uid,String password,String nickname){

System.out.println("--uid--"+uid+"--password--"+"--nickname--"+nickname);

String token;

HttpPut httpput = new HttpPut(userurl+"/"+uid);

        Map<String,String> params = new HashMap<String,String>();

        //params.put("username", uid);

        params.put("password", password);  

        params.put("nickname", nickname); 

List<NameValuePair> nvps = new ArrayList<NameValuePair>();  

   nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

try {

token = getToken();

nvps.add(new BasicNameValuePair("Authorization", "Bearer "+token));

} catch (ClientProtocolException e) {

e.printStackTrace();

System.out.println("--ClientProtocolException--"+e.getMessage());

} catch (IOException e) {

e.printStackTrace();

System.out.println("--IOException--"+e.getMessage());

}

for(NameValuePair nameValuePair:nvps){

httpput.addHeader(nameValuePair.getName(), nameValuePair.getValue());

        }

DefaultHttpClient client = new DefaultHttpClient();

try {

httpput.setEntity(new StringEntity(JsonUtils.pojo2json(params),"utf-8"));

HttpResponse response = client.execute(httpput);

if(response != null){

int statusCode = response.getStatusLine().getStatusCode();

if(statusCode == 200){

System.out.println("--put--");

}

}

String retSrc = EntityUtils.toString(response.getEntity());

System.out.println("---success---"+retSrc);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}


/**

* get

* @author hekang

* @param uid

* 获取单个用户信息

*/

public void getHuanXinUser(String uid){

String token;

HttpResponse httpResponse;

HttpGet httpGet = new HttpGet(userurl+"/"+uid);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();  

        nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

try {

token = getToken();

nvps.add(new BasicNameValuePair("Authorization", "Bearer "+token));

} catch (ClientProtocolException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

        for(NameValuePair nameValuePair:nvps){

        httpGet.addHeader(nameValuePair.getName(), nameValuePair.getValue());

        }

        try {

httpResponse = new DefaultHttpClient().execute(httpGet);

if(httpResponse != null){

int statusCode = httpResponse.getStatusLine().getStatusCode();

if(statusCode == 200){

System.out.println("--get--");

}

}

String retSrc = EntityUtils.toString(httpResponse.getEntity());

System.out.println("----success----"+retSrc);

/*JSONObject json = new JSONObject(retSrc);

if("service_resource_not_found".equals(json.get("error").toString())){

System.out.println("--环信服务器不存在该用户--");

}*/

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* @author hekang

* 获取该app下的所有环信用户

*/

public String getAllHuanXinUsers(){

String token;

HttpResponse httpResponse;

BufferedReader in = null;  

String content = null; 

HttpGet httpGet = new HttpGet(userurl);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();  

        nvps.add(new BasicNameValuePair("Content-Type", "application/json"));

try {

token = getToken();

nvps.add(new BasicNameValuePair("Authorization", "Bearer "+token));

} catch (ClientProtocolException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

        for(NameValuePair nameValuePair:nvps){

        httpGet.addHeader(nameValuePair.getName(), nameValuePair.getValue());

        }

        try {

httpResponse = new DefaultHttpClient().execute(httpGet);

in = new BufferedReader(new InputStreamReader(httpResponse.getEntity()  

.getContent()));

StringBuffer sb = new StringBuffer("");  

String line = "";  

String NL = System.getProperty("line.separator");  

while ((line = in.readLine()) != null) {  

                sb.append(line + NL);  

            } 

in.close();

content = sb.toString();  

System.out.println("---allusers---"+content);

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally {  

            if (in != null) {  

                try {  

                    in.close();// 最后要关闭BufferedReader  

                } catch (Exception e) {  

                    e.printStackTrace();  

                }  

            }  

        }

return content;  

       

}

}



本文转自 沉淀人生 51CTO博客,原文链接:http://blog.51cto.com/825272560/1862543
相关文章
|
1月前
|
Java 数据库 Android开发
不同主题增删改查系统【纯控制台】(Java课设)
不同主题增删改查系统【纯控制台】(Java课设)
15 1
|
2月前
Servlet使用适配器模式进行增删改查案例(IDeptService.java)
Servlet使用适配器模式进行增删改查案例(IDeptService.java)
15 0
|
2月前
Servlet使用适配器模式进行增删改查案例(EmpDaoImpl.java)
Servlet使用适配器模式进行增删改查案例(EmpDaoImpl.java)
14 0
|
2月前
Servlet使用适配器模式进行增删改查案例(DeptDaoImpl.java)
Servlet使用适配器模式进行增删改查案例(DeptDaoImpl.java)
13 0
|
2月前
Servlet使用适配器模式进行增删改查案例(IDeptDao.java和IEmpDao.java)
Servlet使用适配器模式进行增删改查案例(IDeptDao.java和IEmpDao.java)
14 0
|
2月前
Servlet使用适配器模式进行增删改查案例(IBaseDaoUtil.java)
Servlet使用适配器模式进行增删改查案例(IBaseDaoUtil.java)
23 0
|
2月前
Servlet使用适配器模式进行增删改查案例(BaseDao.java)
Servlet使用适配器模式进行增删改查案例(BaseDao.java)
16 0
|
2月前
Servlet使用适配器模式进行增删改查案例(Dept.java)
Servlet使用适配器模式进行增删改查案例(Dept.java)
19 0
|
4月前
|
SQL Java 数据库连接
java链接hive数据库实现增删改查操作
java链接hive数据库实现增删改查操作
156 0
|
2月前
Servlet使用适配器模式进行增删改查案例(Emp.java)
Servlet使用适配器模式进行增删改查案例(Emp.java)
13 0