结合Servlet、传递JSON数据,实现信鸽Java服务端向Android客户端(参考这里)推送数据
环境
服务器:winserver 2012、eclipse、tomcat
客户端 :Mac、Android Studio
1 Android端与服务端通信
1.1 服务端
1.1.1 搭建JavaWeb环境
参考:部署tomcat
参考:配置Server
参考:eclipse配置Tomcat
1.1.2 新建项目
服务端新建JavaWeb项目XGServer,新建JsonServlet继承HttpServlet
package com.Servlet;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tencent.xingeUtil.Demo;
public class JsonServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
request = req;
try {
request.setCharacterEncoding("utf-8");
// 获取客户端post请求发送的Json数据
InputStream inputStream = request.getInputStream();
String json = NetUtils.readString(inputStream);
json = URLDecoder.decode(json, "UTF-8");
System.out.println(json+"111");
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置web.xml
<servlet>
<servlet-name>JsonServletservlet-name>
<servlet-class>com.Servlet.JsonServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>JsonServletservlet-name>
<url-pattern>/JsonServleturl-pattern>
servlet-mapping>
这样通过http://服务器IP:8080/XGServer/JsonServlet,就能访问到Servlet
1.2 Android 端
public static void sendJson(String content){
String path="Java端Servlet访问地址";
JSONObject json=new JSONObject();
HttpURLConnection conn=null;
try {
json.put("客户端",content);
conn= (HttpURLConnection) new URL(path).openConnection();
conn.setRequestMethod("POST");//post请求
conn.setRequestProperty("ser-Agent", "Fiddler");
conn.setRequestProperty("Content-Type", "application/json");
conn.setConnectTimeout(5 * 1000);
// 包装并上传数据
OutputStream outputStream = conn.getOutputStream();
outputStream.write(URLEncoder.encode(json.toString(), "UTF-8").getBytes());
outputStream.flush();
outputStream.close();
if (HttpURLConnection.HTTP_OK == conn.getResponseCode())
Log.i("flag","连接成功");
else
Log.i("flag","连接失败:"+conn.getResponseCode());
} catch (Exception e) {
e.printStackTrace();
}
finally {
conn.disconnect();
}
}
2 推送消息
SDK下载选择JavaSDK。
解压后:
将PushJavaSDK.Jar复制到服务端的项目XGServer中,Add to Build Path。打开Demo.java类,里面封装了很多方法,分析其中一个方法如下:
在XGServer中也新建一个Demo类
ACCESS ID与SECRET KEY获取,参考: 腾讯信鸽推送Android端
public class Demo {
private static XingeApp xinge = new XingeApp(ACCESS ID , "SECRET KEY");
//下发单个账号
public static JSONObject demoPushSingleAccount(String str) {
Message message = new Message();
message.setExpireTime(86400);
message.setTitle("title");
message.setContent("content");
message.setType(Message.TYPE_NOTIFICATION);
Style style = new Style(0,1,1,1,0,1,1,0);
message.setStyle(style);
JSONObject ret = xinge.pushSingleAccount(0, "account", message);
System.out.println(ret);
return ret;
}
}
在JsonServlet中调用demoPushSingleAccount方法,便可以向所有账号为account的设备发送推送。
具体API请参考:信鸽服务端Java SDK V1.1.8用户手册
下面分析一下XingApp、Message、Style这三个
XingeApp
该类 供与信鸽后台交互的接口。构造函数有两个参数,均为必选。
参数名 | 类型 | 必需 | 默认值 | 参数描述 |
---|---|---|---|---|
accessId | int | 是 | 无 | 推送目标应用 id |
secretKey | String | 是 | 无 | 推送密钥 |
注:secretKey 是后台接口鉴权的密钥,accessKey 为客户端鉴权密钥。调用后台 sdk 时需要使用 secretKey。 Example:
XingeApp push = new XingeApp(000, "myKey");
Message
定义推送消息(Android 平台)
Example:
Message mess = new Message(); mess.setType(Message.TYPE_NOTIFICATION);
mess.setTitle("title");
mess.setContent("中午");
mess.setExpireTime(86400);
//依次为(int builderId, int ring, int vibrate, int clearable, int nId, int lights, int iconType,
int styleId)
Style style = new Style(0,0,0,1,0,1,0,1);
style.setIconRes("xg.png");
XingeApp.pushSingleAccount
推送消息给单个账号
public JSONObject pushSingleAccount(int deviceType, String account, Message message) //向Android客户端发送消息
参数名 | 类型 | 必需 | 默认值 | 参数描述 |
---|---|---|---|---|
deviceType | int | 否 | 0 | 请填0 |
account | string | 是 | 无 | 要与Android端一致,Android端才能收到推送 |
message | Message | 是 | 无 |
Example:
XingeApp push = new XingeApp(000, 'myKey');
Message mess = new Message(); //$mess = new MessageIOS();
//完善 Message 消息
...
JSONObject ret = push. pushSingleAccount (0, 'nickName', mess);
Return value:
{"ret_code":0} //成功
{"ret_code":-1, "err_msg":"error description"}
注:ret_code 为 0 表示成功,其他为失败