Send JSON data via HTTP POST using spring android
I am using spring android framework for retrieving json data via Http POST. The parameters which are sent to Server via http body; got null.
Code:
ResponseEntity<LoginBean> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, LoginBean.class);
While calling this method; in requestEntity both http header and body are found but when actually call is made to the server(via Spring API) the parameters are found null at server end.
The code for bean is as follow :
Code:
public class LoginBean {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
Android code is as follow :
Code:
protected String doInBackground(String... params) {
String username = params[0];
String password = params[1];
String url = connectServices.connectLoginServiceURL();
MultiValueMap<String, String> loginServiceParam = new LinkedMultiValueMap<String, String>();
loginServiceParam.add("username", username);
loginServiceParam.add("password", password);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(loginServiceParam, requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Add the Gson message converters
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP POST request, marshaling the response from JSON
ResponseEntity<LoginBean> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, LoginBean.class);
LoginBean loginBeanResponse = responseEntity.getBody();
status = loginBeanResponse.getStatus();
return status;
}
Please suggest some solution.
Thanks!!