Mieux encore (IMO) utilise Spring for AndroidRestTemplate
en conjonction avec AndroidAnnotations@Rest
interface.
De cette façon, vous pouvez simplifier un appel d'API encore plus de ceci: (par exemple à partir du printemps pour Android page d'accueil)
// The connection URL
String url = "https://ajax.googleapis.com/ajax/" +
"services/search/web?v=1.0&q={query}";
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP GET request, marshaling the response to a String
String result = restTemplate.getForObject(url, String.class, "Android");
à ceci:
@Rest(converters = {MappingJackson2HttpMessageConverter.class, StringHttpMessageConverter.class},
rootUrl = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}")
String getResult(String query);
//usage
String foo = getResult("bar");
Merci @cipley – Naruara