404使用Retrofit发出POST请求时

时间:2016-10-16 23:19:39

标签: android rest post retrofit2

我使用Retrofit2创建了以下API调用:

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

我试着像这样使用那个方法:

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

这是日志显示的内容:

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

我搜索过其他StackOverflow答案,常见的嫌疑人是基本网址。我的访问方式如下:

Task

此API调用的主体应该是我想要创建的{ "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 } ] } } 。响应应该是以下形式:

status

哪个是:doc::后端是否有错误,AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimsIdentity.DefaultNameClaimType; 刚刚创建的任务

我不知道为什么会这样。我确定基本URL是正确的。我实际上可以使用REST API test app from the Play Store并使用空主体执行POST请求,并且响应是正确的。只有当我在Android上尝试这个时才会出现此错误。

我会感激任何帮助。

1 个答案:

答案 0 :(得分:0)

原来我是个白痴。我需要删除@POST参数的结尾反斜杠。

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