我是使用 RestTemplates 的新手。在我的应用程序中,我尝试使用 post 方法发送请求,如下所示
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("application","json",Charset.forName("UTF-8")));
requestHeaders.add("username", "sai3");
requestHeaders.add("password", "x");
requestHeaders.add("device_id", device_id);
HttpEntity<String> requestEntity = new HttpEntity<String>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
` restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
ResponseEntity<GetResponseBean> responseEntity1 = restTemplate.exchange(url, HttpMethod.POST, requestEntity, GetResponseBean.class);
obj = responseEntity1.getBody();
Log.v("GetDataResponse", "result message : "+obj);
对于我使用的上述代码"spring-android-core-1.0.1.RELEASE.jar"
,"spring-android-rest-template-1.0.1.RELEASE.jar"
以及"jackson-all-1.9.8.jar"
如果我使用ResponseEntity
type is String 那么它会返回很好的响应,但是如果我更改ResponseEntity
typeGetResponseBean
类型,那么我会收到如下错误:
Caused by: org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.example.springsxmlposting.GetResponseBean] and content type [text/html]
GetResponseBean
类如下:
public class GetResponseBean {
String result = null;
String message = null;
public void setmResult(String result) {
this.result = result;
}
public String getmResult() {
return result;
}
public void setmMessage(String message) {
this.message = message;
}
public String getmMessage() {
return message;
}
}
我怎样才能将数据放入我的 bean 类(GetResponseBean
)请任何人帮助我