2017-08-15 84 views
1

我有一個REST web服務,它允許我通過POST請求以JSON格式上傳用戶詳細信息。它看起來像我能做到這一點使用Codenameone如何在POST查詢中添加多個JSON子條目

post.addArgument("Name",entry.get("Name").toString()); 
post.addArgument("JobRole",entry.get("JobRole").toString()); 

「項」是一個ArrayList < MapString,Object>中

正如你可以在下面的JSON我也有每個用戶發送多個條目的選項見(在這種情況下,詳細地址)如本JSON例如:

{ 

    "Name":"Fred Flintstone", 

    "JobRole":"Quarry worker", 

    "Address":[ 

    { 

     "Address1" :"Boulder House", 

     "Address2" :"Rock Way", 

     "Address3" :"Rock City" 

    } 

    ] 
} 

我使用已經試過

post.addArgumentArray("Address",entry.get("Address1").toString,entry.get("Address2")) 

在地址下組合用戶的條目,但我得到「400:錯誤請求」返回。那麼如何將這樣的多個條目添加到我的請求中?

問候

回答

1

那些是POST類型的參數,它們被添加爲普通的HTTP參數不是JSON(這就像在提交HTML表單)。

你在找什麼是一樣的東西:

ConnectionRequest cr = new ConnectionRequest(url, true) { 
    protected void buildRequestBody(OutputStream os) throws IOException { 
     // snipped this but you should get the rest... 
     os.write("{\"Name\":\"Fred Flintstone\",\"JobRole\":\"Quarry worker\", ..."); 
    } 
}; 

或者您可以使用new terse REST API

Map<String, Object> jsonData = Rest.post(myUrl).body(bodyValueAsString).getAsJsonMap();