检索使用Spring Android框架通过HTTP POST JSON数据 [英] Retrieving JSON data via HTTP POST using Spring Android Framework
本文介绍了检索使用Spring Android框架通过HTTP POST JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的春天Android框架通过HTTP POST检索JSON数据。不过,虽然消费的服务,在服务器端的参数接收空。
I am using spring android framework for retrieving json data via Http POST. But while consuming the service, at server side the parameter are received null.
下面是Android code:
Following is the android code :
protected String doInBackground(String... params) {
String username = params[0];
String password = params[1];
String url = connectServices.connectLoginServiceURL();// returns url
loginServiceParam = new LinkedMultiValueMap<String, String>();
loginServiceParam.add("username", username);
loginServiceParam.add("password", password); //username and password are null at server end.
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
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;
}
LoginBean类是以下内容:
LoginBean class is following :
public class LoginBean {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
JSON的反应是:
The json response is :
{"status":"true"}
谢谢!
推荐答案
我自己解决了这个查询。
继code需要放在
I myself resolved this query. Following code needs to be put
RestTemplate restTemplate = new RestTemplate(true);
真正需要POST请求。
默认情况下它是假的,这是对GET请求。
这甚至不是在春天的Android参考链接提及一>
这篇关于检索使用Spring Android框架通过HTTP POST JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文