binbinyang-- Retrofit 2.0的简单使用(入门篇)-GET请求
2017-07-12 10:51
513 查看
最近在看 Retrofit !打算自己把 Retrofit 跟RXjava 跟OKHTTP融合一起。。慢慢一步步 自己搭建一个框架,自己抽取基类
话不多说。直接进入主题
官方文档
使用之前当然需要导入依赖库
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 通过? & 连接查询条件 (也就是键值对)
这是请求后的数据
首先需要定义一个接口
在Activity中
话不多说。直接进入主题
官方文档
使用之前当然需要导入依赖库
compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'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()); } }); } }
相关文章推荐
- Retrofit的简单使用(入门篇)-GET请求
- retrofit2.0的简单使用
- Android Retrofit 2.0 简单使用
- Android中Retrofit2.0的简单使用
- Retrofit2.0的简单使用 (包含Gson)
- Retrofit 2.0非常简单的入门(翻译官方文档)
- Retrofit2.0的简单使用
- Retrofit2.0详解(一简单使用)
- Retrofit简单入门及使用
- Retrofit2.0简单使用总结
- 学习笔记——Retrofit2.0的简单使用
- Retrofit 2.0关于get和post的网络访问的简单使用
- Retrofit2.0的简单使用
- Retrofit2.0 简单使用总结
- retrofit 2.0 简单使用
- Retrofit入门[简单使用方式介绍哦]
- Android中网络框架Retrofit2.0简单使用
- android 三剑客(Retrofit 2.0)简单使用
- 天气预报项目学习总结(- ButterKnife - Retrofit 2.0(okhttp) - Rxjava - Jackson - Ormlite - Mosby简单使用总结)
- 简单入门使用Retrofit+ rxjava 下载图片