Retrofit基础之最简单的get请求方式
标签:网络框架
添加依赖
compile 'com.squareup.retrofit2:retrofit:2.x.x'
compile 'com.squareup.retrofit2:converter-gson:2.x.x' //这个也必须加进去,这是一坑
用法
- 本例子以聚合数据天气预报API接口测试。
- 接口基地址为:(http://v.juhe.cn/weather/)而根据城市名/id查询天气是在基地址后面加一段(index)
- 代码中 WeatherResp 为数据结构原型,根据自己的数据体建一个Bean放进去就OK了
1、首先先建一个接口,可以理解为URL自由拼接的接口
public interface WeatherService {
//@GET会被识别为get请求
//index会拼接到BASE_URL中,此时这个URL已经拼接完毕
@GET("index") //@Query会被识别为往完整URL中附带参数(index?key=xxx&cityname=xx&···)
Call<WeatherResp> cityNameQueryWeather(@Query("key") String key,
@Query("cityname") String cityname,
@Query("format") int format);