【问题标题】:retrofit.RetrofitError: 404 Not Foundretrofit.RetrofitError: 404 Not Found
【发布时间】:2015-06-27 23:13:39
【问题描述】:

我正在开发一个利用 Retrofit 进行 api 调用的 android 应用程序。当我调用以下代码时:

String searchText = query;
RestAdapter restAdapter = new RestAdapter.Builder()
        .setEndpoint("https://mashape-community-urban-dictionary.p.mashape.com")
        .build();
WordsApiService wordsApi = restAdapter.create(WordsApiService.class);
wordsApi.getWordDefinition(query, new Callback<WordsResponse>() {
    @Override
    public void success(WordsResponse wordsResponse, Response response) {
        System.out.println(response.toString());
    }

    @Override
    public void failure(RetrofitError error) {
        System.out.println(error.getResponse().getStatus());
    }
});

使用以下服务:

public interface WordsApiService {

    @Headers({
            "X-Mashape-Key : *insert real api key*",
            "Accept: text/plain"
    })
    @GET("/define/")
    void getWordDefinition(@Query("word") String word, Callback<WordsResponse> callback);

}

我收到 404 错误(retrofit.RetrofitError: 404 Not Found)。
当我进入 mashape 时,我看到服务调用正在通过并命中 api。

在应用程序的配置方面我是否遗漏了一些明显的东西?

【问题讨论】:

    标签: android api service get retrofit


    【解决方案1】:

    "define" endpoint 路径中删除正斜杠。
    另外,查询参数是“term”而不是“word”

    public interface WordsApiService {
        @Headers({
            "X-Mashape-Key : *insert real api key*",
            "Accept: text/plain"
        })
        @GET("/define")
        void getWordDefinition(@Query("term") String term, Callback<WordsResponse> callback);
    }
    

    【讨论】:

    • 谢谢!那成功了。我真的希望有更多关于 Retrofit 的文档。作为一个 api 消费的菜鸟,感觉就像我在黑暗中蹒跚而行。
    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2020-08-19
    • 2017-05-11
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多