Jump to content

Poster des données JSON


Recommended Posts

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

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

  • 2 weeks later...

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

  • 2 weeks later...

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...