Il y a une bibliothèque étonnamment agréable pour HTTP Android disponible sur le lien ci-dessous:
http://loopj.com/android-async-http/
demandes simples sont très faciles:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
}
});
Pour envoyer JSON (crédit ` voidberg 'au https://github.com/loopj/android-async-http/issues/125):
// params is a JSONObject
StringEntity se = null;
try {
se = new StringEntity(params.toString());
} catch (UnsupportedEncodingException e) {
// handle exceptions properly!
}
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
client.post(null, "www.example.com/objects", se, "application/json", responseHandler);
Tout est asynchrone, fonctionne bien avec Android et peut être appelé depuis votre thread d'interface utilisateur. Le responseHandler s'exécutera sur le même thread que vous l'avez créé (généralement, votre thread UI). Il a même une réponse intégrée pour JSON, mais je préfère utiliser google gson.
Est-postMessage un objet JSON? – AndroidDev
Le travail est comme de la magie! –
'postMessage' n'est pas défini – Raptor