How to use POST key / value pairs instead of a JSON object using Spring for Android?

I am trying to use Spring for Android to make a standard HTTP POST for a URL where the body is just a list of parameters (such as key-value pairs) and not a JSON object.

I would like the response to be converted from JSON to Java ResponseObject, but from what I can say, Spring will also convert my body to JSON.

Here is my code:

Map<String, Object> params = new HashMap<String, Object>(); params.put("client_id", mClientId); params.put("state", mState); params.put("username", mUsername); params.put("password", mPassword); return getRestTemplate().postForObject(url, params, ResponseObject.class); 

Thank you in advance!

+4
source share
2 answers

try it

publishing a simple list of name pairs

 // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); try{ List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("name", nameString)); nameValuePairs.add(new BasicNameValuePair("Country", "country name")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclient.execute(httppost); }catch (ClientProtocolException e) { // TODO Auto-generated catch block } 
+3
source

Use .exchange()

 // Create the request body as a MultiValueMap MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>(); body.add("client_id", mClientId); // and so on // Note the body object as first parameter! HttpEntity<?> httpEntity = new HttpEntity<Object>(body, requestHeaders); MyModel model = restTemplate.exchange("/api/url", HttpMethod.POST, httpEntity, MyModel.class); 
+2
source

All Articles