我正在尝试使用Rest网络服务从我的android应用添加新用户,该服务是通过spring实现的。 这是我的代码:
final String url = getString(R.string.uri) + "/addPatient";
ObjectMapper mapper = new ObjectMapper();
String userJson = null;
try {
userJson = mapper.writeValueAsString(Patient);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
RestTemplate restTemplate = new RestTemplate();
try {
ResponseEntity<String> response
= restTemplate.postForEntity(url, userJson, String.class);
if (response.getStatusCode() == HttpStatus.CREATED) {
Log.i("rr", "rrop created");
success = true;
} else if (response.getStatusCode() == HttpStatus.CONFLICT) {
Log.i("rr", "rrop user already exist");
}
//return response.getBody();
} catch (HttpStatusCodeException e) {
if (Integer.parseInt(e.getStatusCode().toString()) == 401) {
Log.i("rr", "rrop error 401");
}
if (Integer.parseInt(e.getStatusCode().toString()) == 404) {
Log.i("rr", "rrop error 404 not found");
}
if (Integer.parseInt(e.getStatusCode().toString()) == 403) {
Log.i("rr", "rrop error 403 not authorized");
}
if (Integer.parseInt(e.getStatusCode().toString()) == 409) {
Log.i("rr", "rrop error 409 conflict");
}
Log.i("rr", "rrop error get" + e.getStatusCode());
} catch (ResourceAccessException e) {
} catch (Exception e) {}
return null;
}
但是我总是遇到相同的错误,就像我即使已授权也无法访问此Web服务:
I / System.out:[socket] []已连接
I / System.out:[CDS] rx超时:0
D / dalvikvm:GC_FOR_ALLOC释放了3484K(62760),18%释放了16624K / 20172K,暂停30ms,总计30ms
I / System.out:[CDS]关闭[60445]
W / RestTemplate:“ mysite / addPatient”的POST请求导致405();调用错误处理程序
I / System.out:关闭[套接字] [/ 0.0.0.0:60445]
I / rr:错误出现405