【发布时间】:2014-11-01 20:14:18
【问题描述】:
我正在尝试在我的 Android 应用中发布资源。 GET 工作没有任何问题。 POST 在我的 android sn-p 末尾执行 response.getStatusLine().getStatusCode() 时返回 404(见下文)
我的 Jersey Web 服务方法如下所示:
@POST
@Path("/create")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response create(@FormParam("name") String name,
@FormParam("description") String description) throws IOException {
User user = new User();
user.setName(name);
user.setDescription(description);
// store object in database...
return Response.status(Status.OK).build();
}
还有android app中的代码:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("**my-url**/create");
ArrayList<NameValuePair> paramspost = new ArrayList<NameValuePair>();
paramspost.add(new BasicNameValuePair("name", "My name"));
paramspost.add(new BasicNameValuePair("description", "My description"));
try {
httpPost.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
httpPost.setEntity(new UrlEncodedFormEntity(paramspost, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最终状态码是 404,我的数据库中没有存储任何内容。我的代码有什么问题?
【问题讨论】:
-
你试过重启你的网络服务吗?
-
嗯,等等。您实际上在哪里将“POST”定义为 HTML 请求方法?
标签: java android jersey http-post jax-rs