2012-03-01 57 views
3

什麼是通過嵌套參數的JSON在這種形式如何在Android中傳遞RPC的Json嵌套參數

{"method":"startSession", 
"params": [ "email": "[email protected]", 
      "password": "1234", 
      "stayLogged": "1", 
      "idClient": "ANDROID" 
      ] 
} 

到接收RPC web服務URL正確的代碼?

web服務代碼

@Webservice(paramNames = {"email", "password", "stayLogged", "idClient"}, 
public Response startSession(String email, String password, Boolean stayLogged, String idClient) throws Exception { 
    boolean rC = stayLogged != null && stayLogged.booleanValue(); 
    UserService us = new UserService(); 
    User u = us.getUsersernamePassword(email, password); 
    if (u == null || u.getActive() != null && !u.getActive().booleanValue()) { 
     return ErrorResponse.getAccessDenied(id, logger); 
    } 
    InfoSession is = null; 
    String newKey = null; 
    while (newKey == null) { 
     newKey = UserService.md5(Math.random() + " " + new Date().getTime()); 
     if (SessionManager.get(newKey) != null) { 
      newKey = null; 
     } else { 
      is = new InfoSession(u, rC, newKey); 
      if (idClient != null && idClient.toUpperCase().equals("ANDROID")) { 
       is.setClient("ANDROID"); 
      } 
      SessionManager.add(newKey, is); 
     } 
    } 
    logger.log(Level.INFO, "New session started: " + newKey + " - User: " + u.getEmail()); 
    return new Response(new InfoSessionJson(newKey, is), null, id); 
} 

回答

2

我會假設你正在使用JSON-RPC 1.0,因爲在你的要求沒有版本的指標。

首先您錯過了您的「ID」,請將其添加到請求中。

現在,您可以嘗試3種不同的東西。

1)如果要設置名稱和值對,則需要使用對象{}而不是array []。 喜歡:

{"method":"startSession", 
"params": { "email": "[email protected]", 
      "password": "1234", 
      "stayLogged": "1", 
      "idClient": "ANDROID" 
      }, 
"id":100 
} 

2)如果您的JSON解串器被要求數組語法[]然後可能必須包住對象{} []中一樣:

{"method":"startSession", 
"params": [{ "email": "[email protected]", 
      "password": "1234", 
      "stayLogged": "1", 
      "idClient": "ANDROID" 
      }], 
    "id":101 
} 

3)最後,你也可以嘗試使用陣列像位置參數:

{"method":"startSession", 
"params": [ "[email protected]", 
      "1234", 
      "1", 
      "ANDROID" 
      ], 
    "id":102 
} 

希望有所幫助。

相關問題