将 Retrofit @GET 与 WeatherAPI 结合使用 - 静态参数 [英] Using Retrofit @GET with a WeatherAPI - static parameters

查看:48
本文介绍了将 Retrofit @GET 与 WeatherAPI 结合使用 - 静态参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenWeather API 并首次尝试 Retrofit.我正在尝试将预测拉出 X 天的时间.可在此处找到预测 API 的文档:

I am using the OpenWeather API and taking my first crack at Retrofit. I am trying to pull the forecast for a period of X number of days. The documentation for a forecast API can be found here:

http://openweathermap.org/forecast16

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

It looks like the relevant link to the API for a particular city's forecast is below:

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 调用的静态部分.这是我所拥有的:

I am a bit confused about how I meet my @GET annotation and the associated method for an asynchronous response. The "&" in the API link is confusing as well since I don't really know I would include a static part of the API call in my @GET annotation. Here is what I have:

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

 }

对于这个特定问题以及如何解决改造的任何帮助,我们将不胜感激.

Any help with this specific problem and how to tackle Retrofit in general would be appreciated.

推荐答案

它应该可以正常工作.Retrofit 处理 & 符号.

It should work fine. Retrofit takes care of the & symbol.

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 端点:

I have an API endpoint like this:

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

这是我的 Retrofit 接口方法:

And this is my Retrofit interface method:

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

这篇关于将 Retrofit @GET 与 WeatherAPI 结合使用 - 静态参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆