【发布时间】:2014-12-14 11:56:39
【问题描述】:
我是一名初级程序员,在学校我必须制作一个 servlet 和一个 Android 应用程序。
服务器使用 JSON 向我的 servlet 发送请求,如下所示:
{
"function":"authenticate",
"requestId":"[random]",
"deviceId":"[android deviceid]",
"serviceType":"GCM"
}
servlet 读取 JSON 并使用 authenticateRequest() 发送通知:
else if(function.equals("authenticate"))
{
// Get the deviceId
String deviceId = jsonRequest.getDeviceId();
// Get the serviceType
String serviceType = jsonRequest.getServiceType();
GCM gcmClass = new GCM();
// Send authentication request to the user
int authenticationResult = 0;
if(serviceType.equals("GCM"))
{
authenticationResult = gcmClass.authenticateRequest(deviceId, requestId);
}
if(serviceType.equals("APNS"))
authenticationResult = 70000;
// Set the result field
jsonResponse.setResult(authenticationResult);
// Set the result text
if(authenticationResult == 0)
{
jsonResponse.setResultText("OK");
}
else if(serviceType.equals(10000))
{
jsonResponse.setResultText("DENY");
}
else if(serviceType.equals(70000))
{
jsonResponse.setResultText("Unsupported");
}
else
{
jsonResponse.setResult(50000);
}
// Set the requestId field
jsonResponse.setRequestId(requestId);
// Send the JSON response
response.getOutputStream().print(gson.toJson(jsonResponse));
response.getOutputStream().flush();
}
authenticateRequest(此时它总是显示状态 0(这意味着总是 ALLOW):
public int authenticateRequest(String regId, String requestId)
{
try
{
String messageText = "New authentication request received!";
Sender sender = new Sender(Config.GOOGLE_SERVER_KEY);
Message message = new Message.Builder().timeToLive(30).delayWhileIdle(true).addData(Config.MESSAGE_KEY, messageText).addData("requestid", requestId).build();
result = sender.send(message, regId, 1);
return 0;
}
catch(Exception e) {
e.printStackTrace();
return 60000;
}
}
现在 Android 应用程序接收到 requestId,我可以使用它向 servlet 发送另一个 JSON HTTP POST 请求。 android 应用需要向 servlet 发送 ALLOW of DENY 并且 servlet 应该在收到第一个请求的同一会话中返回它。
但我似乎无法弄清楚如何做到这一点,现在卡在这一点上几天了。
如果有人能帮我解决这个问题,我将不胜感激。
【问题讨论】:
标签: java android json servlets google-cloud-messaging