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 66 67 68 69 70 71 72 73
| @Override
public String doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0], params[1], params[2], params[3]);
return null;
}
public void onPostExecute() {
//Toast.makeText(getApplicationContext(), objPostIdNum.result, Toast.LENGTH_LONG).show();
}
public String postData(String precisionP, String dateP, String positionPLa, String positionPLo) {
try {
adresseUrlPost = "http://192.168.107.14:146/api/PositionGPS/42202b162";
// 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);
String json = "";
// 3. Construction d'un jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("PrecisionPosition", precisionP);
jsonObject.accumulate("dateHeurePosition", dateP);
jsonObject.accumulate("Position.Latitude", positionPLa);
jsonObject.accumulate("Position.Longitude", positionPLo);
// 4. Convertion JSONObject de JSON en 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. Executer une requete POST en lui donnant l' 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;
} |
Partager