我是一名初级程序员,在学校我必须制作一个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(这意味着总是允许):
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 DENY,并且servlet应该在收到第一个请求的同一会话中返回它。
但我似乎无法弄清楚如何做到这一点,现在已经停留了几天。
我会非常感激有人可以帮助我。
答案 0 :(得分:0)
我自己找到了答案! :d
这就是我想要的:
class Authenticate {
boolean flag = true;
String finalUserInput = null;
public synchronized String sendAuthentication(String deviceId, String requestId)
{
// Send notification
GCM gcmClass = new GCM();
gcmClass.authenticateRequest(deviceId, requestId);
while(flag)
{
try
{
wait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = true;
notify();
return finalUserInput;
}
public synchronized void receiveAuthentication(String userInput, String requestId) {
finalUserInput = userInput;
flag = false;
notify();
}
}
class T1 implements Runnable {
Authenticate m;
private final String deviceId;
private final String requestId;
String result;
public T1(Authenticate m1, String deviceId, String requestId)
{
this.m = m1;
this.deviceId = deviceId;
this.requestId = requestId;
Thread t1 = new Thread(this, "sendAuthentication");
t1.start();
// Wait for thread to finish before sending response
try
{
t1.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
public void run()
{
result = m.sendAuthentication(deviceId, requestId);
}
public String getResult(){
return result;
}
}
class T2 implements Runnable {
Authenticate m;
private final String requestId;
private final String userInput;
public T2(Authenticate m2, String requestId, String userInput) {
this.m = m2;
this.requestId = requestId;
this.userInput = userInput;
Thread t2 = new Thread(this, "receiveAuthentication");
t2.start();
}
public void run() {
m.receiveAuthentication(userInput, requestId);
}
}
public class AuthenticationHandler {
final static Authenticate m = new Authenticate();
public static String sendRequest(String deviceId, String requestId)
{
T1 runnable = new T1(m, deviceId, requestId);
String result = runnable.getResult();
return result;
}
public static void receiveResponse(String requestId, String userInput)
{
new T2(m, requestId, userInput);
}
}