Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Poster des données JSON

Featured Replies

Posté(e)

Bonjour à tous,

Est que vous pouvez m'expliquer comment je pourrai poster des données au format json sur un serveur.

Merci d'avance.

Modifié par nathalie85

Posté(e)

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;
}

Modifié par peKKa

Posté(e)
  • Auteur

Merci pekka pour ta réponse, peux tu m'expliquer un peu comment utiliser ce code stp.

Modifié par nathalie85

Posté(e)

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.

  • 2 weeks later...
Posté(e)

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)

  • 2 weeks later...
Posté(e)

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%

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.