Android HTTP PUT未向服务器发送JSON请求,导致不允许使用HTTP 405方法。
以下是我的异步任务背景代码
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut("URL");
String jsonresponse = "";
try {
StringEntity se = new StringEntity(gson.toJson(resultPojo).toString());
se.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httpPut.setEntity(se);
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httpPut);
HttpEntity httpEntity = response.getEntity();
jsonresponse = EntityUtils.toString(httpEntity);
System.out.println("res .... "+jsonresponse);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
服务器端代码:
@POST
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response submitUserResponse(@PathParam("userId") int userId,
@PathParam("id") int id, List<ResultPojo> responses) {
try {
//core logic goes here
return Response.status(Response.Status.CREATED).build();
} catch (Exception e) {
e.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
答案 0 :(得分:1)
好的就像所讨论的那样,很可能是不匹配不同的HTTP方法,在这种情况下是一个Put和一个帖子,每当你遇到HTTP代码(405)对你使用的方法执行验证时,它就会发生POST方法经常出现.405错误。您可能正在尝试在网站上引入某种输入表单,但并非所有ISP都允许处理表单所需的POST方法。请求的资源不支持请求方法;例如,表单上的GET请求需要通过POST显示数据,或者在只读资源上显示PUT请求。