简体   繁体   English

Android HttpPost to Jersey Web服务返回404

[英]Android HttpPost to Jersey web service returns 404

I am trying to post a resource within my android app. 我正在尝试在我的Android应用程序中发布资源。 GET works without any problems. GET可以正常工作。 POST on the other sides returns a 404 when executing response.getStatusLine().getStatusCode() in the end of my android snippet (see below) 在我的android代码段的末尾执行response.getStatusLine().getStatusCode()时,另一侧的POST返回404(请参见下文)

My Jersey web service method looks like this: 我的Jersey网络服务方法如下所示:

@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();
}

And the code in the android app: 以及android应用中的代码:

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();
        }

The status code is 404 in the end and nothing is stored in my database. 最后的状态码是404,我的数据库中什么也没有存储。 What's wrong with my code? 我的代码有什么问题?

From what I can see, you did never define a request method. 据我所知,您从未定义请求方法。

httpPost.setRequestMethod("POST");

This should solve your problem. 这应该可以解决您的问题。

In your request you're specifying Content-Type: "application/x-www-form-urlencoded;charset=UTF-8", however in your resource you consume "MediaType.APPLICATION_FORM_URLENCODED", which is I beleive without the charset extension. 在您的请求中,您指定了Content-Type:“ application / x-www-form-urlencoded; charset = UTF-8”,但是在您的资源中,您使用的是“ MediaType.APPLICATION_FORM_URLENCODED”,我相信没有charset扩展名。 This may cause troubles, so there is no resource found, which can consume your request. 这可能会引起麻烦,因此没有找到可以消耗您的请求的资源。 Try to omit the charset property from the request header. 尝试从请求标头中省略charset属性。

Also there is an issue with jersey and charset property in Content-Type. Content-Type中的jersey和charset属性也存在问题。 Have a look here 在这里看看

It turned out that my changes didn't deploy correctly to Tomcat . 原来,我的更改未正确部署到Tomcat I deletet everything from my Tomcat folder and deployed again (It looks like creating a war via eclipse doesn't always undeploy correctly). 我从Tomcat文件夹中删除了所有内容,然后再次部署了(通过eclipse创建战争似乎并不总是正确地取消部署)。

This means that the code presented in my questions works fine like this. 这意味着我的问题中呈现的代码可以像这样正常工作。 And I found out that removing the specifed content-type doesn't make any difference. 而且我发现删除指定的内容类型没有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM