Retrofit 框架使用非常简单,只要了解接口注解配置即可:
将网址转换陪接口注解配置
接口1:
接口地址 :http://apis.baidu.com/apistore/weatherservice/citylist
请求方法 :GET
请求参数(header) :
参数名 | 类型 | 必填 | 参数位置 | 描述 | 默认值 |
---|---|---|---|---|---|
string | 是 | header |
请求参数(urlParam) :
参数名 | 类型 | 必填 | 参数位置 | 描述 | 默认值 |
---|---|---|---|---|---|
string | 是 | urlParam |
接口2
接口地址 :http://apis.baidu.com/avatardata/mingrenmingyan/lookup
请求方法 :GET
请求参数(header) :
参数名 | 类型 | 必填 | 参数位置 | 描述 | 默认值 |
---|---|---|---|---|---|
string | 是 | header |
请求参数(urlParam) :
package com.ethanco.retrofit2_0test; import retrofit.Call; import retrofit.http.GET; import retrofit.http.Header; import retrofit.http.Query; import rx.Observable; /** * Created by Administrator on 2016/4/22. */ public interface APIService { //接口说明文档 http://apistore.baidu.com/apiworks/servicedetail/112.html @GET("/apistore/weatherservice/citylist") Call<Weather> getWeather(@Header("apiKey") String apiKey,@Query("cityname") String keyword); //返回回调 /** * retrofit 支持 rxjava 整合 * 这种方法适用于新接口 */ @GET("/apistore/weatherservice/citylist") Observable<Weather> getWeatherData(@Header("apiKey") String apiKey,@Query("cityname") String keyword); //返回Observable //接口说明文档 http://apistore.baidu.com/apiworks/servicedetail/1756.html @GET("/avatardata/mingrenmingyan/lookup") Call<FamousInfo> getFamousResult(@Header("apiKey") String apiKey, @Query("keyword") String keyword, @Query("page") int page, @Query("rows") int rows); }