2014-10-19 93 views
0

我是一名開始的程序員,對於學校我必須製作一個servlet和一個Android應用程序。結合到JSON HTTP POST請求?

服務器發送一個請求到我的servlet使用JSON這樣的:

{ 
"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; 
    } 
} 

現在th e Android應用程序接收到requestId,並且我可以使用它發送另一個JSON HTTP POST請求到servlet。 android應用程序需要發送一個允許DENY到servlet,並且該servlet應該在收到第一個請求的同一會話中返回它。

但我似乎無法弄清楚如何做到這一點,現在已經停留了兩天。

我非常感謝有人能幫助我。

回答

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); 
    } 
}