【问题标题】:Using Retrofit @GET with a WeatherAPI - static parameters使用带有 WeatherAPI 的 Retrofit @GET - 静态参数
【发布时间】:2016-01-30 07:47:48
【问题描述】:

我正在使用 OpenWeather API,并在 Retrofit 上进行了第一次尝试。我正在尝试将预测拉到 X 天。可以在此处找到预测 API 的文档:

http://openweathermap.org/forecast16

看起来特定城市预测的 API 的相关链接如下:

api.openweathermap.org/data/2.5/forecast/daily?q={city name},{country code}&cnt={cnt}

我的基本网址是:

api.openweathermap.org/data/2.5/forecast/daily?

我对如何满足 @GET 注释以及异步响应的关联方法感到有些困惑。 API 链接中的“&”也令人困惑,因为我真的不知道我会在 @GET 注释中包含 API 调用的静态部分。这是我所拥有的:

public interface WeatherAPI {

@GET("/forecast/daily?")
void getResponse(@Query("city")String city, @Query("country_code") int countryCode, @Query("number_of_days") int number_of_days, Callback<List<WeatherForecast>> response);

 }

对于这个特定问题以及如何解决整体改造问题的任何帮助将不胜感激。

【问题讨论】:

    标签: android api retrofit


    【解决方案1】:

    它应该可以正常工作。 Retrofit 会处理 &amp; 符号。

    public interface WeatherAPI {
        @GET("/forecast/daily?")
        void getResponse(
                        @Query("city") String city,
                        @Query("country_code") int countryCode,
                        @Query("number_of_days") int number_of_days,
                        Callback<List<WeatherForecast>> response
        );    
    }
    

    我有一个这样的 API 端点:

    http://myserver.com/api/posts/?page_size=5&page=1
    

    这是我的 Retrofit 接口方法:

    @GET("/posts/")
    void getPostPage(
                     @Query("page") int page,
                     @Query("page_size") int pageSize,
                     Callback<PostPage> callback
    );
    

    【讨论】:

    • 谢谢!我会给它一个测试,让你知道它是否有效!我是编程新手,所以 Retrofit 有点高级,但它似乎封装了很多我之前读过的原始 AsyncTask 类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    相关资源
    最近更新 更多