我正在开发一个连接到 Web 服务的 Android 应用程序。从网络服务我得到这个错误:
POST request for "xxx" resulted in 422 (Unprocessable Entity); invoking error handler
我正在使用适用于 Android 的 SpringFramework Rest Client,并使用以下代码连接到 Web 服务:
public static User sendUserPersonalData(User userProfileData)
{
try
{
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Collections.singletonList(new MediaType("application","json")));
HttpEntity<User> requestEntity = new HttpEntity<User>(userProfileData, requestHeaders);
GsonHttpMessageConverter messageConverter = new GsonHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(messageConverter);
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setReadTimeout(readTimeout);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.setMessageConverters(messageConverters);
String url = URL_BASE_WEB + USER_PERSONAL_DATA_CALL;
ResponseEntity<User> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, User.class);
return responseEntity.getBody();
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
但是,Web 服务也返回一个像这样的 JSON 字符串:
{
"email": [
"is invalid"
],
"birthday": [
"is invalid"
],
"startday": [
"is invalid"
],
"sex_preference": [
"can't be blank"
],
"password": [
"is too long (maximum is 4 characters)"
]
}
即使我得到一个例外,我怎么能得到它?