package com.hpm.blog.jdpush.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.hpm.blog.jdpush.utils.CheckSumBuilder;
import com.hpm.blog.mapper.UserMapper;
public class YunXinServerImpl {
private static final Logger LOGGER = LoggerFactory.getLogger(YunXinServerImpl.class);
public static String register(String username) {
// TODO Auto-generated method stub
String accid= username;
DefaultHttpClient httpClient = new DefaultHttpClient();
String url ="https://api.netease.im/nimserver/user/create.action";
HttpPost httpPost = new HttpPost(url);
String appKey ="xxxxxxxxx";
String appSecret = "xxxxxxxxx";
String nonce = "12345";
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));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回结果
String result = "";
try {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
public static String registupdate(String username) {
// TODO Auto-generated method stub
String accid= username;
DefaultHttpClient httpClient = new DefaultHttpClient();
String url ="https://api.netease.im/nimserver/user/refreshToken.action";
HttpPost httpPost = new HttpPost(url);
String appKey ="xxxxxxxxxx";
String appSecret = "xxxxxxxx";
String nonce = "12345";
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));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回结果
String result = "";
try {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
public static String registerUpdate(String phoneNuber,String nickName,String avatar) {
// TODO Auto-generated method stub
String accid= phoneNuber;
String name = nickName;
String icon = avatar;
DefaultHttpClient httpClient = new DefaultHttpClient();
String url ="https://api.netease.im/nimserver/user/updateUinfo.action";
HttpPost httpPost = new HttpPost(url);
String appKey ="xxxxxxxx";
String appSecret = "xxxxxxxxx";
String nonce = "12345";
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("name", name));
nvps.add(new BasicNameValuePair("icon", icon));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行请求
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 返回结果
String result = "";
try {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(result);
return result;
}
/* public UserVo saveAccidAndToken(String username, String accid, String token) {
UserVo user = userMapper.findByUsername(username);
user.setAccid(accid);
user.setToken(token);
return userMapper.save(user);
}*/
}
由于前端安卓那边说云信聊天和百度地图公用一个插件起冲突,最后导致项目放弃已经做好的云信改用极光IM。
所有如果聊天中要用到地图导航功能的话,用云信可能会出现我们团队遇到的情况。