【问题标题】:Combine to JSON HTTP POST requests?结合 JSON HTTP POST 请求?
【发布时间】: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


    【解决方案1】:

    我自己找到了答案! :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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-04
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      相关资源
      最近更新 更多