java实现注册环信账号_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 createMap = new HashMap();

createMap.put("client_id", client_id);

createMap.put("client_secret", client_secret);

createMap.put("grant_type", grant_type);

HttpPost httpPost = new HttpPost(tokenurl);

List nvps = new ArrayList();

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 params = new HashMap();

params.put("username", uid);

params.put("password", PlainPassword);

params.put("nickname", realName);

HttpPost httpPost = new HttpPost(userurl);

List nvps = new ArrayList();

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 nvps = new ArrayList();

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 params = new HashMap();

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

params.put("password", password);

params.put("nickname", nickname);

List nvps = new ArrayList();

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 nvps = new ArrayList();

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 nvps = new ArrayList();

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值