nathalie85 Posted February 7, 2011 Share Posted February 7, 2011 Bonjour à tous, Est que vous pouvez m'expliquer comment je pourrai poster des données au format json sur un serveur. Merci d'avance. Link to comment Share on other sites More sharing options...
peKKa Posted February 7, 2011 Share Posted February 7, 2011 J'utilise ce code pour récupérer/poster des données facilement au format json. Je l'ai repris de quelqu'un mais j'ai perdu la source désolé... public static String getData(String Key){ String temp1=""; HttpPost httppost = new HttpPost("http://appinvtinywebdb.appspot.com/getvalue"); HttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("tag", Key)); httpclient.getParams().setParameter("http.useragent", "TinyWebDB/Android dataget"); try { httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response=httpclient.execute(httppost); temp1 = EntityUtils.toString(response.getEntity()); if(D) Log.i(TAG+" - getData", temp1); //int statuscode = response.getStatusLine().getStatusCode(); }catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block //lblErrorMessage.setText("HTTP IO Exception"); e.printStackTrace(); } String val = "non"; // Decode the JSON array. Array is zero based so the return value is in element 2 try { JSONArray jsonArray = new JSONArray(temp1); val = jsonArray.getString(2); } catch (JSONException e) { // TODO Auto-generated catch block //lblErrorMessage.setText("Error in JSON decoding"); e.printStackTrace(); } return val; } Link to comment Share on other sites More sharing options...
nathalie85 Posted February 7, 2011 Author Share Posted February 7, 2011 Merci pekka pour ta réponse, peux tu m'expliquer un peu comment utiliser ce code stp. Link to comment Share on other sites More sharing options...
peKKa Posted February 7, 2011 Share Posted February 7, 2011 Merci pekka pour ta réponse, peux tu m'expliquer un peu comment ça marche ce code stp. On se connecte à une url, ou j'ai un service web tout simple : je lui passe une clé et il me renvoi une valeur le tout au format json. Je suis pas un expert JSON donc c'est tout ce que je peux te dire... Mais j'espère que ce code te servira de piste. Link to comment Share on other sites More sharing options...
nathalie85 Posted February 7, 2011 Author Share Posted February 7, 2011 Ok merci je vais essayer. Link to comment Share on other sites More sharing options...
throrin19 Posted February 17, 2011 Share Posted February 17, 2011 bonjour, est-il possible de faire un Service local permettant de faire cette action? J'en aurai besoin pour un traitement pendant un spashscreen et lors d'un click sur un bouton de réactalisation dans une autre activity. (j'essai de chercher la meilleure façon de réutiliser facilement du code) Link to comment Share on other sites More sharing options...
hocinao Posted March 4, 2011 Share Posted March 4, 2011 Bonjour , Si tu as des données dans une Map , tu peux construire ton JSONObject avec l'instruction suivante : JSONObject jsonObject = new JSONObject(maMap); Apres ; tu encapsule ton objet json dans une String Entity comme suivant : HTTPost httpPost = new HttpPost(url); StringEntity stringEntity = new StringEntity(json.toString()); httpPost.setEntity(stringEntity); et après , exécuter la requete avec ton HTTPClient : HTTPClient client = new DefaultHttpClient(); client.execute(httpPost); j'espère que c'est claire j'ai utilisé cette méthode pour envoyer un objet JSON dans une requete avec la methode post , et ça marche 100% Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.