Réponse d'une requête POST JSON
Bonjour,
Pour mon projet scolaire je dois effectuer des requetes http et particulierement une requete post en format JSON, la requete fonctionne bien et je reçois une réponse qui est un id et un numéro de téléphone le probléme c'est que ma réponse est un string et j'aimerais obtenir d'un coté mon id et d'un autre mon numéro sans à avoir à découper le string avec substring, j'aimerais donc savoir comment je peux faire.
voici le code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public String postData(String telephoneP, String mdpP) {
try {
//Composition de l'adresse url avec la valeur de l'edit text et du num de téléphone récupéré
adresseUrlPost="http://192.168.117.14:146/api/Psurveille";
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(adresseUrlPost);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("telephone", telephoneP);
jsonObject.accumulate("password", mdpP);
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "probleme, ne fonctionne pas";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
} |
Merci