2017-07-07 5 views
0

ich hier ein Beispiel, das ich brauche, um zu bauen zu bauen:wie JSONObject Anfrage zu verwenden, in Httppost

{ 
    "sendSmsRequest": { 
    "to": "5511982694404", 
    "msg": "funcionou" 
    } 
} 

Um dies zu tun, ich habe verwendet JSONObject:

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("to", "123456789"); 
jsonObject.put("msg", "Mensagem Teste"); 
StringEntity input = new StringEntity(jsonObject.toString()); 

Hier die Anfrage:

post.setHeader("Accept", "application/json"); 
post.setHeader("Content-Type", "application/json"); 
post.setEntity(input); 

Aber ich weiß nicht, wie die „Header“ setzen - „sendSmsRequest“ ... Es gibt eine Möglichkeit, tue dies ohne Verwendung von String ??

String teste = "{\"sendSmsRequest\": { \"to\": \"123456789\",\"msg\": \"funcionou\"}}"; 

Antwort

1

Sie können es wie folgt tun:

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("to", "123456789"); 
jsonObject.put("msg", "Mensagem Teste"); 
JSONObject jsonObject1 = new JSONObject(); 
jsonObject1.put("sendSmsRequest", jsonObject); 

StringEntity input = new StringEntity(jsonObject1.toString()); 

Like this Sie eine JSONObject innerhalb eines anderen JSONObject haben.

+0

Danke Moondaisy, das ist genau das, was ich brauche !! –