Retrofit基础之最简单的get请求方式
2016-12-29 15:54
232 查看
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); //format是非必要参数,可以用重写 }
2、再建一个类,将基本的进行封装,如不需要用户输入的KEY
//根据城市名查询天气 public class HttpModel { //接口地址相同的部分抽取出来作为BASE_URL private static final String BASE_URL = "http://v.juhe.cn/weather/"; //聚合平台分配的key,抽取出来 private static final String KEY = "------------------------"; //实例化一个gson private static Gson gson = new Gson(); //针对部分不需要用户输入的参数进行内部自行解决,如key public static Call<WeatherResp> cityNameToQuery(String cityName, int format) { //通过建造者模式 Builder链式构造出Retrofit实例 Retrofit retrofit = new Retrofit.Builder() //首先设置BASE_URL,会与index进行拼接URL .baseUrl(BASE_URL) /*这个非常重要,官方说默认是gson,我当时以为不用写这个,结果一直报错 *所以还是要写,这个时候用的就是依赖的第二个包 */ .addConverterFactory(GsonConverterFactory.create(gson)) //然后通过build构建完成,此时就是一个Retrofit实例 .build(); //然后通过Retrofit实例creat出WeatherService接口实例。 WeatherService service = retrofit.create(WeatherService.class); //拿到接口实例,去调用接口内的方法,返回值是一个回调接口Call Call<WeatherResp> call = service.cityNameQueryWeather(KEY, cityName, format); //将回调接口Call返回出去 return call; } //针对上面的方法重写变为一个参数,format平台默认为1,也可以忽略用户未输入 public static Call<WeatherResp> cityNameToQuery(String cityName) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); WeatherService service = retrofit.create(WeatherService.class); Call<WeatherResp> call = service.cityNameQueryWeather(KEY, cityName, 1); return call; } }
3、开始请求数据
//在MainActivity中开始请求数据 //enqueue是异步请求方式,添加回调返回数据 HttpModel.cityNameToQuery("武汉").enqueue(new Callback<WeatherResp>() { @Override public void onResponse(Call<WeatherResp> call, Response<WeatherResp> response) { //response.body才是数据原型 Log.d("body", response.body().toString()); } @Override public void onFailure(Call<WeatherResp> call, Throwable t) { } });
更方便写法
1、接口WeatherServicepublic interface WeatherService { @GET("index") Call<WeatherResp> cityNameQueryWeather(@Query("key") String key, @Query("cityname") String cityname, 4000 @Query("format") int format); public class BuildRetrofit { private static final String BASE_URL = "http://v.juhe.cn/weather/"; private static Gson gson = new Gson(); public static WeatherService retrofit() { WeatherService service = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build() //实例化Retrofit 再去创建WeatherService实例 .create(WeatherService.class); return service; } } }
2、封装类HttpModel
//根据城市名查询天气 public class HttpModel { private static final String KEY = "-----------------------"; public static Call<WeatherResp> cityNameToQuery(String cityName, int format) { Call<WeatherResp> call = WeatherService.BuildRetrofit.retrofit().cityNameQueryWeather(KEY, cityName, format); return call; } public static Call<WeatherResp> cityNameToQuery(String cityName) { Call<WeatherResp> call = WeatherService.BuildRetrofit.retrofit().cityNameQueryWeather(KEY, cityName, 1); return call; } }
3、开始请求网络
HttpModel.cityNameToQuery("武汉").enqueue(new Callback<WeatherResp>() { @Override public void onResponse(Call<WeatherResp> call, Response<WeatherResp> response) { Log.d("body", response.body().toString()); } @Override public void onFailure(Call<WeatherResp> call, Throwable t) { } });
结后语
至此Retrofit最简单的GET请求就是这样了,相信大家能举一反三相关文章推荐
- Retrofit简单封装使用--Post请求封装请求体、Get请求通过参数、map方式构建url
- 初学者---Android (简单总结)Retrofit请求方式之get,post
- [厉害了,我的JAVA基础](八)HTTP请求的GET与POST方式的区别
- Retrofit(一)简单实用get方式
- [Android] Retrofit2.0 GET和POST方式请求
- Retrofit2.0的get post请求简单数据
- java最简单的方式实现http get请求 gbk转utf8 javaweb发布到服务器
- retrofit简单的get请求
- Android使用HttpClient完成Post和Get方式的联网请求
- Ajax的GET与POST方式向服务器发送请求
- 维吉尼亚密码--在简单加密方式凯撒密码基础上的一个改进
- C# wnform 请求http ( get , post 两种方式 )
- Tomcat 中 GET方式 请求 中文乱码 出现的原因
- 简单使用ajax(get方式)
- forward与get/post请求方式-重庆的那孩子的问题
- get请求的乱码解决方式
- tomcat下get方式提交请求乱码解决办法
- ajax通过get方式发送请求
- ajax通过get方式发送请求
- ajax打开请求的两种方式(get,post)