简体   繁体   English

404使用翻新发出POST请求时

[英]404 When making POST request using Retrofit

I have the following API call I created using Retrofit2: 我有使用Retrofit2创建的以下API调用:

@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/create/")
Call<StandardTaskResponse> createNewTask(@Body Task newTask);

and I try to use that method like so: 我尝试像这样使用该方法:

Task newTask = new Task(name, description, "", null, USER, 4, priority, null, null);

      Call<StandardTaskResponse> call = HavocService.getInstance().getHavocAPI().createNewTask(newTask);
        call.enqueue(new Callback<StandardTaskResponse>() {
            @Override
            public void onResponse(Call<StandardTaskResponse> call, Response<StandardTaskResponse> response) {
                LogUtil.v("Response message: " + response.message());
            }

            @Override
            public void onFailure(Call<StandardTaskResponse> call, Throwable t) {
                LogUtil.e(t.getCause().getMessage());
            }
        });

This is what the logs show: 这是日志显示的内容:

10-16 19:08:07.963 10511-10894/io.havoc.todo D/OkHttp: --> POST http://ec2-amazonaws-base-url-redacted.com:3000/api/task/create/ http/1.1
10-16 19:08:07.964 10511-10894/io.havoc.todo D/OkHttp: Content-Type: application/json
10-16 19:08:07.964 10511-10894/io.havoc.todo D/OkHttp: Content-Length: 124
10-16 19:08:07.964 10511-10894/io.havoc.todo D/OkHttp: Accept: application/json
10-16 19:08:07.964 10511-10894/io.havoc.todo D/OkHttp: {"category":"","description":"H","indexInList":4,"name":"H","priority":"HIGH","user":"57a7bd24-ddf0-5c24-9091-ba331e486dc7"}
10-16 19:08:07.964 10511-10894/io.havoc.todo D/OkHttp: --> END POST (124-byte body)
10-16 19:08:07.969 10511-10516/io.havoc.todo I/art: Do partial code cache collection, code=23KB, data=29KB
10-16 19:08:07.969 10511-10516/io.havoc.todo I/art: After code cache collection, code=23KB, data=29KB
10-16 19:08:07.969 10511-10516/io.havoc.todo I/art: Increasing code cache capacity to 128KB
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: <-- 404 Not Found http://ec2-amazonaws-base-url-redacted.com:3000/api/task/create/ (34ms)
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: content-type: application/json; charset=utf-8
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: cache-control: no-cache
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: vary: accept-encoding
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: Date: Sun, 16 Oct 2016 23:07:59 GMT
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: Connection: keep-alive
10-16 19:08:07.999 10511-10894/io.havoc.todo D/OkHttp: Transfer-Encoding: chunked
10-16 19:08:08.020 10511-10894/io.havoc.todo D/OkHttp: {"statusCode":404,"error":"Not Found"}
10-16 19:08:08.020 10511-10894/io.havoc.todo D/OkHttp: <-- END HTTP (38-byte body)
10-16 19:08:08.050 10511-10511/io.havoc.todo V/(NewTaskActivityPresenter.java:40): Response message: Not Found

I have searched around on other StackOverflow answers and the common suspect is the base URL. 我搜索了其他StackOverflow答案,最常见的怀疑是基本URL。 Mine is accessed like so: 像这样访问我的:

private static final String HAVOC_URI = "http://ec2-amazonaws-base-url-redacted.com:3000/api/"; .

The body for this API call is just supposed to be the Task I want to create. 该API调用的主体只是应该是我要创建的Task The response is supposed to be something of the form: 响应应该是以下形式:

{
    "status":true,
    "doc":{
        "_id":"5801175bc5c3f451301fd235",
        "t_id":"8671fc295bc9",
        "name":"Do calculus homework",
        "description":"Finish all assigned homework from chapters 1 and 2",
        "category":"test",
        "indexInList":0,
        "priority":3,
        "dateDue":1477291500000,
        "user":"57a",
        "status":"ARCHIVED",
        "__v":0,
        "subtasks":[
            {
                "name":"Finish Chapter 1 - Derivatives",
                "isCompleted":false
            },
            {
                "name":"Finish Chapter 1 - Integrals",
                "isCompleted":false
            },
            {
                "name":"Finish Chapter 2 - Graphing",
                "isCompleted":false
            }
        ]
    }
}

Which is: status : whether or not there was an error on the backend, doc: task that was just created 这是: status :后端是否有错误, doc:刚刚创建的任务

I have no clue why this is happening. 我不知道为什么会这样。 I'm certain the base URL is correct. 我确定基本URL是正确的。 I can actually use a REST API test app from the Play Store and do a POST request with an empty body and the response is correct. 实际上,我可以使用Play商店中REST API测试应用程序,并使用空白内容进行POST请求,并且响应正确。 It's only when I attempt this on Android do I get this error. 只有在Android上尝试此操作时,才会出现此错误。

I'd appreciate any and all help. 我将不胜感激。

Turns out I'm an idiot. 原来我是个白痴。 I needed to remove the ending backslash for the @POST parameter. 我需要删除@POST参数的结尾反斜杠。

@Headers({"Accept: application/json", "Content-Type: application/json"})
@POST("task/create")
Call<StandardTaskResponse> createNewTask(@Body Task newTask);

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

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