**
Java后端集成融云
**
首先个人感觉融云在即时通讯和推送方面,app端采用融云即时通讯的话,推送用友盟是不行的,安卓端集成这两个sdk会冲突。
发送广播(单播)
public static void broadcast(String value,String user) {
String broadcast = "https://api-cn.ronghub.com/push.json";
StringBuffer res = new StringBuffer();
String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳,从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。
String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数,无长度限制。
String Signature = sha1(App_Secret + Nonce + Timestamp);//数据签名。
Map<String, String> map = new HashMap<>();
map.put("App-Key", App_Key);
map.put("Timestamp", Timestamp);
map.put("Nonce", Nonce);
map.put("Signature", Signature);
map.put("Content-Type", "application/json");
JSONArray userid=new JSONArray();
userid.add(user);
JSONObject jsonObject=new JSONObject();
JSONObject audience=new JSONObject();
audience.put("is_to_all","false");
audience.put("userid",userid);
JSONArray platform=new JSONArray();
platform.add("ios");
platform.add("android");
JSONObject notification=new JSONObject();
notification.put("alert","this is a push");
String content1="{'content':'"+value+"','extra':'aa'}";
JSONObject content=new JSONObject();
content.put("content",content1);
content.put("objectName","RC:TxtMsg");
JSONObject message=new JSONObject();
jsonObject.put("message",content);
jsonObject.put("platform",platform);
jsonObject.put("fromuserid","1");
jsonObject.put("audience",audience);
jsonObject.put("notification",notification);
String s = jsonObject.toString();
String httpsResponseContent = null;
try {
httpsResponseContent = HttpSendUtil.getHttpsResponseContent(broadcast, map, jsonObject.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
value 是你要发送的内容
user 是你要发送的人的id
App_Key 融云的appkey
HttpSendUtil
融云的是https协议,普通的http请求无法调用
public class HttpSendUtil {
/**
* http请求发送
*
* @param url
* 地址
* @param header
* http请求头信息,用Map类型来存储
* @param ctnt
* 请求报文
* @return 请求结果
*/
public static HttpResponse sendHttpRequest(String url, Map<String, String> header, String ctnt) throws Exception {
@SuppressWarnings("resource")
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if (header != null && header.size() > 0) {
for (String key : header.keySet()) {
post.addHeader(key, header.get(key));
}
}
byte[] requestBytes = String.valueOf(ctnt).getBytes("GBK");
InputStream is = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
NameValuePair pair = new NameValuePair() {
public String getValue() {
return "GBK";
}
public String getName() {
return "charset";
}
};
HttpEntity entity = new InputStreamEntity(is, requestBytes.length,
ContentType.create("application/x-www-form-urlencoded", pair));
post.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(post);
return httpResponse;
}
/**
* https请求发送
*
* @param url
* 地址
* @param header
* http请求头信息,用Map类型来存储
* @param ctnt
* 请求报文
* @return 请求结果
*/
public static HttpResponse sendHttpsRequest(String url, Map<String, String> header, String ctnt) throws Exception {
HttpClient httpClient = getNewHttpClient();
SSLContext ctx = SSLContext.getInstance("SSL");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ClientConnectionManager ccm = httpClient.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
HttpPost post = new HttpPost(url);
if (header != null && header.size() > 0) {
for (String key : header.keySet()) {
post.addHeader(key, header.get(key));
}
}
byte[] requestBytes = String.valueOf(ctnt).getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);
NameValuePair pair = new NameValuePair() {
public String getValue() {
return "UTF-8";
}
public String getName() {
return "charset";
}
};
HttpEntity entity = new InputStreamEntity(is, requestBytes.length,
ContentType.create("application/x-www-form-urlencoded", pair));
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
return response;
}
/**
* 发送HTTPS请求,直接返回响应报文
*
* @param url
* 请求路径
* @param header
* 投格式
* @param ctnt
* 请求报文
* @return 请求结果
*/
public static String getHttpsResponseContent(String url, Map<String, String> header, String ctnt) throws Exception {
HttpResponse response = sendHttpsRequest(url, header, ctnt);
return getResponseContent(response);
}
/**
* 发送HTTP请求,直接返回响应报文
*
* @param url
* 请求路径
* @param header
* 投格式
* @param ctnt
* 请求报文
* @return 返回结果
*/
public static String getHttpResponseContent(String url, Map<String, String> header, String ctnt) throws Exception {
HttpResponse response = sendHttpRequest(url, header, ctnt);
return getResponseContent(response);
}
/**
*
* @param response
* response
* @return string
*/
private static String getResponseContent(HttpResponse response) throws Exception {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
if (statusCode == 302 || statusCode == 301) {
String location = response.getHeaders("Location")[0].getValue();
String cookie = response.getHeaders("Set-Cookie")[0].getValue();
return location + "###" + cookie;
} else {
throw new Exception("HTTP请求状态:" + statusCode);
}
}
return EntityUtils.toString(response.getEntity());
}
/**
*
* @return DefaultHttpClient
*/
private static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
}
-希望帮助到大家