binbinyang-- Retrofit 2.0的简单使用(入门篇)-GET请求
来源:互联网 发布:c语言什么时候用void 编辑:程序博客网 时间:2024/06/10 21:00
最近在看 Retrofit !打算自己把 Retrofit 跟RXjava 跟OKHTTP融合一起。。慢慢一步步 自己搭建一个框架,自己抽取基类
话不多说。直接进入主题
官方文档
使用之前当然需要导入依赖库
- 1
- 2
- 3
- 1
- 2
- 3
首先这个是我用百度api,聚合函数弄来的接口请求
http://v.juhe.cn/weather/index?format=1&cityname=%E6%80%80%E5%8C%96&key=c1b298ee331cd0aa1767470559303f26
是一个GET请求
其中
http://v.juhe.cn 是网站地址
/weather/index 是数据获取接口(查询数据是通过访问servlet链接获取)
?format=1&cityname=%E6%80%80%E5%8C%96&key=c1b298ee331cd0aa1767470559303f26 通过? & 连接查询条件 (也就是键值对)
这是请求后的数据
{resultcode: "200",reason: "successed!",result: {sk: {temp: "25",wind_direction: "西南风",wind_strength: "1级",humidity: "87%",time: "09:21"},today: {temperature: "25℃~30℃",weather: "多云",weather_id: {fa: "01",fb: "01"},wind: "南风微风",week: "星期三",city: "怀化",date_y: "2017年07月12日",dressing_index: "较舒适",dressing_advice: "建议着薄外套或牛仔衫裤等服装。年老体弱者宜着夹克衫、薄毛衣等。昼夜温差较大,注意适当增减衣服。",uv_index: "中等",comfort_index: "",wash_index: "较适宜",travel_index: "适宜",exercise_index: "适宜",drying_index: ""},future: {day_20170712: {temperature: "25℃~30℃",weather: "多云",weather_id: {fa: "01",fb: "01"},wind: "南风微风",week: "星期三",date: "20170712"},day_20170713: {temperature: "25℃~32℃",weather: "多云转晴",weather_id: {fa: "01",fb: "00"},wind: "南风微风",week: "星期四",date: "20170713"},day_20170714: {temperature: "25℃~33℃",weather: "多云转晴",weather_id: {fa: "01",fb: "00"},wind: "南风微风",week: "星期五",date: "20170714"},day_20170715: {temperature: "25℃~33℃",weather: "多云",weather_id: {fa: "01",fb: "01"},wind: "南风微风",week: "星期六",date: "20170715"},day_20170716: {temperature: "25℃~34℃",weather: "晴",weather_id: {fa: "00",fb: "00"},wind: "南风微风",week: "星期日",date: "20170716"},day_20170717: {temperature: "25℃~33℃",weather: "多云转晴",weather_id: {fa: "01",fb: "00"},wind: "南风微风",week: "星期一",date: "20170717"},day_20170718: {temperature: "25℃~33℃",weather: "多云",weather_id: {fa: "01",fb: "01"},wind: "南风微风",week: "星期二",date: "20170718"}}},error_code: 0}
需要先创建一个Bean类,可以手写,也可以使用GsonFormat生成,此类就是json转换成的java类,叫WeatherEntity吧
/** * Created by yangbin on 2017/7/10. */public class WeatherEntity { private String resultcode; private String reason; private ResultBean result; private int error_code; public String getResultcode() { return resultcode; } public void setResultcode(String resultcode) { this.resultcode = resultcode; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } public ResultBean getResult() { return result; } public void setResult(ResultBean result) { this.result = result; } public int getError_code() { return error_code; } public void setError_code(int error_code) { this.error_code = error_code; } public static class ResultBean { /** * sk : {"temp":"25","wind_direction":"西南风","wind_strength":"1级","humidity":"87%","time":"09:21"} * today : {"temperature":"25℃~30℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期三","city":"怀化","date_y":"2017年07月12日","dressing_index":"较舒适","dressing_advice":"建议着薄外套或牛仔衫裤等服装。年老体弱者宜着夹克衫、薄毛衣等。昼夜温差较大,注意适当增减衣服。","uv_index":"中等","comfort_index":"","wash_index":"较适宜","travel_index":"适宜","exercise_index":"适宜","drying_index":""} * future : {"day_20170712":{"temperature":"25℃~30℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期三","date":"20170712"},"day_20170713":{"temperature":"25℃~32℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"南风微风","week":"星期四","date":"20170713"},"day_20170714":{"temperature":"25℃~33℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"南风微风","week":"星期五","date":"20170714"},"day_20170715":{"temperature":"25℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期六","date":"20170715"},"day_20170716":{"temperature":"25℃~34℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"南风微风","week":"星期日","date":"20170716"},"day_20170717":{"temperature":"25℃~33℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"南风微风","week":"星期一","date":"20170717"},"day_20170718":{"temperature":"25℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期二","date":"20170718"}} */ private SkBean sk; private TodayBean today; private FutureBean future; public SkBean getSk() { return sk; } public void setSk(SkBean sk) { this.sk = sk; } public TodayBean getToday() { return today; } public void setToday(TodayBean today) { this.today = today; } public FutureBean getFuture() { return future; } public void setFuture(FutureBean future) { this.future = future; } public static class SkBean { /** * temp : 25 * wind_direction : 西南风 * wind_strength : 1级 * humidity : 87% * time : 09:21 */ private String temp; private String wind_direction; private String wind_strength; private String humidity; private String time; public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getWind_direction() { return wind_direction; } public void setWind_direction(String wind_direction) { this.wind_direction = wind_direction; } public String getWind_strength() { return wind_strength; } public void setWind_strength(String wind_strength) { this.wind_strength = wind_strength; } public String getHumidity() { return humidity; } public void setHumidity(String humidity) { this.humidity = humidity; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } } public static class TodayBean { /** * temperature : 25℃~30℃ * weather : 多云 * weather_id : {"fa":"01","fb":"01"} * wind : 南风微风 * week : 星期三 * city : 怀化 * date_y : 2017年07月12日 * dressing_index : 较舒适 * dressing_advice : 建议着薄外套或牛仔衫裤等服装。年老体弱者宜着夹克衫、薄毛衣等。昼夜温差较大,注意适当增减衣服。 * uv_index : 中等 * comfort_index : * wash_index : 较适宜 * travel_index : 适宜 * exercise_index : 适宜 * drying_index : */ private String temperature; private String weather; private WeatherIdBean weather_id; private String wind; private String week; private String city; private String date_y; private String dressing_index; private String dressing_advice; private String uv_index; private String comfort_index; private String wash_index; private String travel_index; private String exercise_index; private String drying_index; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBean getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBean weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDate_y() { return date_y; } public void setDate_y(String date_y) { this.date_y = date_y; } public String getDressing_index() { return dressing_index; } public void setDressing_index(String dressing_index) { this.dressing_index = dressing_index; } public String getDressing_advice() { return dressing_advice; } public void setDressing_advice(String dressing_advice) { this.dressing_advice = dressing_advice; } public String getUv_index() { return uv_index; } public void setUv_index(String uv_index) { this.uv_index = uv_index; } public String getComfort_index() { return comfort_index; } public void setComfort_index(String comfort_index) { this.comfort_index = comfort_index; } public String getWash_index() { return wash_index; } public void setWash_index(String wash_index) { this.wash_index = wash_index; } public String getTravel_index() { return travel_index; } public void setTravel_index(String travel_index) { this.travel_index = travel_index; } public String getExercise_index() { return exercise_index; } public void setExercise_index(String exercise_index) { this.exercise_index = exercise_index; } public String getDrying_index() { return drying_index; } public void setDrying_index(String drying_index) { this.drying_index = drying_index; } public static class WeatherIdBean { /** * fa : 01 * fb : 01 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class FutureBean { /** * day_20170712 : {"temperature":"25℃~30℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期三","date":"20170712"} * day_20170713 : {"temperature":"25℃~32℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"南风微风","week":"星期四","date":"20170713"} * day_20170714 : {"temperature":"25℃~33℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"南风微风","week":"星期五","date":"20170714"} * day_20170715 : {"temperature":"25℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期六","date":"20170715"} * day_20170716 : {"temperature":"25℃~34℃","weather":"晴","weather_id":{"fa":"00","fb":"00"},"wind":"南风微风","week":"星期日","date":"20170716"} * day_20170717 : {"temperature":"25℃~33℃","weather":"多云转晴","weather_id":{"fa":"01","fb":"00"},"wind":"南风微风","week":"星期一","date":"20170717"} * day_20170718 : {"temperature":"25℃~33℃","weather":"多云","weather_id":{"fa":"01","fb":"01"},"wind":"南风微风","week":"星期二","date":"20170718"} */ private Day20170712Bean day_20170712; private Day20170713Bean day_20170713; private Day20170714Bean day_20170714; private Day20170715Bean day_20170715; private Day20170716Bean day_20170716; private Day20170717Bean day_20170717; private Day20170718Bean day_20170718; public Day20170712Bean getDay_20170712() { return day_20170712; } public void setDay_20170712(Day20170712Bean day_20170712) { this.day_20170712 = day_20170712; } public Day20170713Bean getDay_20170713() { return day_20170713; } public void setDay_20170713(Day20170713Bean day_20170713) { this.day_20170713 = day_20170713; } public Day20170714Bean getDay_20170714() { return day_20170714; } public void setDay_20170714(Day20170714Bean day_20170714) { this.day_20170714 = day_20170714; } public Day20170715Bean getDay_20170715() { return day_20170715; } public void setDay_20170715(Day20170715Bean day_20170715) { this.day_20170715 = day_20170715; } public Day20170716Bean getDay_20170716() { return day_20170716; } public void setDay_20170716(Day20170716Bean day_20170716) { this.day_20170716 = day_20170716; } public Day20170717Bean getDay_20170717() { return day_20170717; } public void setDay_20170717(Day20170717Bean day_20170717) { this.day_20170717 = day_20170717; } public Day20170718Bean getDay_20170718() { return day_20170718; } public void setDay_20170718(Day20170718Bean day_20170718) { this.day_20170718 = day_20170718; } public static class Day20170712Bean { /** * temperature : 25℃~30℃ * weather : 多云 * weather_id : {"fa":"01","fb":"01"} * wind : 南风微风 * week : 星期三 * date : 20170712 */ private String temperature; private String weather; private WeatherIdBeanX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanX { /** * fa : 01 * fb : 01 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class Day20170713Bean { /** * temperature : 25℃~32℃ * weather : 多云转晴 * weather_id : {"fa":"01","fb":"00"} * wind : 南风微风 * week : 星期四 * date : 20170713 */ private String temperature; private String weather; private WeatherIdBeanXX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanXX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanXX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanXX { /** * fa : 01 * fb : 00 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class Day20170714Bean { /** * temperature : 25℃~33℃ * weather : 多云转晴 * weather_id : {"fa":"01","fb":"00"} * wind : 南风微风 * week : 星期五 * date : 20170714 */ private String temperature; private String weather; private WeatherIdBeanXXX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanXXX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanXXX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanXXX { /** * fa : 01 * fb : 00 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class Day20170715Bean { /** * temperature : 25℃~33℃ * weather : 多云 * weather_id : {"fa":"01","fb":"01"} * wind : 南风微风 * week : 星期六 * date : 20170715 */ private String temperature; private String weather; private WeatherIdBeanXXXX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanXXXX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanXXXX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanXXXX { /** * fa : 01 * fb : 01 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class Day20170716Bean { /** * temperature : 25℃~34℃ * weather : 晴 * weather_id : {"fa":"00","fb":"00"} * wind : 南风微风 * week : 星期日 * date : 20170716 */ private String temperature; private String weather; private WeatherIdBeanXXXXX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanXXXXX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanXXXXX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanXXXXX { /** * fa : 00 * fb : 00 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class Day20170717Bean { /** * temperature : 25℃~33℃ * weather : 多云转晴 * weather_id : {"fa":"01","fb":"00"} * wind : 南风微风 * week : 星期一 * date : 20170717 */ private String temperature; private String weather; private WeatherIdBeanXXXXXX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanXXXXXX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanXXXXXX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanXXXXXX { /** * fa : 01 * fb : 00 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } public static class Day20170718Bean { /** * temperature : 25℃~33℃ * weather : 多云 * weather_id : {"fa":"01","fb":"01"} * wind : 南风微风 * week : 星期二 * date : 20170718 */ private String temperature; private String weather; private WeatherIdBeanXXXXXXX weather_id; private String wind; private String week; private String date; public String getTemperature() { return temperature; } public void setTemperature(String temperature) { this.temperature = temperature; } public String getWeather() { return weather; } public void setWeather(String weather) { this.weather = weather; } public WeatherIdBeanXXXXXXX getWeather_id() { return weather_id; } public void setWeather_id(WeatherIdBeanXXXXXXX weather_id) { this.weather_id = weather_id; } public String getWind() { return wind; } public void setWind(String wind) { this.wind = wind; } public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public static class WeatherIdBeanXXXXXXX { /** * fa : 01 * fb : 01 */ private String fa; private String fb; public String getFa() { return fa; } public void setFa(String fa) { this.fa = fa; } public String getFb() { return fb; } public void setFb(String fb) { this.fb = fb; } } } } }}
首先需要定义一个接口
/** * Created by yangbin on 2017/7/10. */public interface WeatherService { @GET("/weather/index") Call<WeatherEntity> getWeatherInfo(@Query("format") int format, @Query("cityname") String cityname, @Query("key") String key);}
在Activity中
public class MainActivity extends AppCompatActivity { @Bind(R.id.click_me_BN) Button mClickMeBN; @Bind(R.id.result_TV) TextView mResultTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.click_me_BN) public void onClick() { getMovie(); } //进行网络请求 private void getMovie() { String baseUrl = "http://v.juhe.cn/"; Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); WeatherService movieService = retrofit.create(WeatherService.class); Call<WeatherEntity> call = movieService.getWeatherInfo(1,"怀化","c1b298ee331cd0aa1767470559303f26"); call.enqueue(new Callback<WeatherEntity>() { @Override public void onResponse(Call<WeatherEntity> call, Response<WeatherEntity> response) { if (response.isSuccessful()) { mResultTV.setText(response.body().getResult().getSk().getHumidity().toString()); } } @Override public void onFailure(Call<WeatherEntity> call, Throwable t) { mResultTV.setText(t.getMessage()); } }); }}
阅读全文
0 0
- binbinyang-- Retrofit 2.0的简单使用(入门篇)-GET请求
- Retrofit的简单使用(入门篇)-GET请求
- retrofit简单的get请求
- Retrofit的基本使用(Get请求)
- Retrofit的(get以及post)的简单使用
- Retrofit基础之最简单的get请求方式
- retrofit网络请求的简单封装使用
- Retrofit的GET请求使用,结合Gson解析
- Retrofit学习(一)集成-简单get请求
- Retrofit(一)、Retrofit+OkHttp实现简单的Get与Post请求
- Kotlin使用Retrofit进行get请求的方法(懒加载机制)
- 快捷使用Retrofit网络请求框架(get请求,post请求,上传文件)
- retrofit简单的网络请求
- Retrofit 2.0关于get和post的网络访问的简单使用
- Retrofit简单封装使用--Post请求封装请求体、Get请求通过参数、map方式构建url
- Retrofit 2.0非常简单的入门(翻译官方文档)
- Retrofit的Get与Post请求
- Retrofit的基本post,get请求
- 资源管理器Assets Manager ---热更新
- SQL Server中角色与权限管理
- Linux Centos7下安装GitLab最详细教程
- mysql中文乱码的解决方法
- MVC模式和三层架构之间的关系
- binbinyang-- Retrofit 2.0的简单使用(入门篇)-GET请求
- JedisCluster源码解读:集群初始化、slot(槽)的分配、值的存取
- grafana+influxdb+python实现监控cpu、内存
- QT HTTP下载网络文件
- 【二叉树】DFS计算路径和,保存路径【113. Path Sum II】【112. Path Sum】
- 记录linux 命令
- MultiValueMap的序列化以及反序列化
- spark架构
- 【编程初学者】创建自己的开源项目5-在主分支上创建新分支(myeclipse+git)