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 57 58 59 60 61 62 63 64 65
| try {
//Composition URL
adresseUrlPost = "http:xxxxxxxxxxxxxxxxxxx";
String json = "";
String pos="";
double posLa,posLo;
// 1. Création de HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. créer la requete POST en lui donnant l'URL
HttpPost httpPost = new HttpPost(adresseUrlPost);
//Convertion d'un string en double
posLa = Double.parseDouble(positionPLa);
posLo = Double.parseDouble(positionPLo);
// 3. Construction d'un jsonObject
JSONObject posObj = new JSONObject();
posObj.accumulate("Latitude",posLa);
posObj.accumulate("Longitude",posLo);
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("PrecisionPosition", precisionP);
jsonObject.accumulate("dateHeurePosition", dateP);
jsonObject.accumulate("Position", posObj);
// 4. Convertion JSONObject de JSON en String
json = jsonObject.toString();
// 5. mettre json dans StringEntity
StringEntity se = new StringEntity(json);
// 6. mettre httpPost Entity
httpPost.setEntity(se);
// 7. Définition des en-têtes pour informer le server
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
// 8. Executer une requete POST en lui donnant l' URL et la chaine Sting
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. Mettre la reponse dans inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convertion inputstream en string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "probleme, ne fonctionne pas";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
//11. convertion string en JSONObject
try {
myObject = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
// 12. retourner resultat
return myObject;
} |
Partager