POST 和 OkHttp3 的 404 错误

404 error with POST and OkHttp3

我在 Android 上使用 OkHttp3 将数据发布到 Challonge API 时遇到问题...这是我的代码的要点:

OkHttpClient client = new OkHttpClient();

HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
urlBuilder = HttpUrl.parse("https://api.challonge.com/v1/tournaments/"+EVENT_ID+".json")
    .newBuilder();

RequestBody postBody = new FormBody.Builder()
    .add("_method", "post")
    .add("api_key", API_KEY)
    .add("participant[name]", name.getText().toString())
    .add("participant[misc]", forum_id.getText().toString())
    .build();

Request request = new Request.Builder()
    .url(urlBuilder.build().toString())
    .post(postBody)
    .build();

Response response = client.newCall(request).execute();

无论我做什么,得到的响应都是 404 页面。

如果我对同一个 URL 执行 GET 响应,我会得到正确的响应。但是,当我在请求中添加 .post(postBody) 时,立即出现 404s.

Challonge API 的文档在这里: http://api.challonge.com/v1/documents/participants/create

在我看来你只是用错了 URL。您得到的 URL "https://api.challonge.com/v1/tournaments/"+EVENT_ID+".json" 是用于检索单个锦标赛的 URL,如 here 所示。此 link 旨在接收 GET 请求。

根据您提供的 link,您应该将代码更改为 POST 至 https://api.challonge.com/v1/tournaments/"+EVENT_ID+"/participants.json