Android-网络框架04Retrofit2.0+RxJava
16lz
2021-01-23
Retrofit官网: https://github.com/square/retrofit
文档 http://square.github.io/retrofit/
本文的意义
随着Google对HttpClient 摒弃,和Volley的逐渐没落,OkHttp开始异军突起,而Retrofit则对okHttp进行了强制依赖。
Retrofit实质上就是对okHttp的封装
现在的Android开发者不会Retrofit + RXJava就感觉跟不上时代了一样,所以小试一下
转载请注明出处 ethan_xue博客
具体步骤
使用方法可查看官网。
按照下面步骤简单几步使用起来。
说明:涉及RXJava的用法直接跳到第5步
(1)添加网络访问权限并添加库依赖
<uses-permission android:name="android.permission.INTERNET" />compile 'com.squareup.retrofit2:retrofit:2.2.0'compile 'com.squareup.retrofit2:converter-gson:2.2.0'compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'compile 'io.reactivex:rxandroid:1.2.1'
(2)建Interface
public interface TestGetService { @GET("adat/sk/{cityId}.html") Call getWeather(@Path("cityId") String cityId);}
(3)异步调用
Retrofit retrofit = new Retrofit.Builder() //这里建议:- Base URL: 总是以/结尾;- @Url: 不要以/开头 .baseUrl("http://www.weather.com.cn/") .build(); TestGetService apiStores = retrofit.create(TestGetService.class); Call call = apiStores.getWeather("101010100"); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { try { Log.e("xue", "response=" + response.body().string()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Call call, Throwable t) { Log.e("xue", "onFailure=" + t.getMessage()); } });
(4)GSON
// 上面的天气:http://www.weather.com.cn/adat/sk/101010100.html
新建bean文件
public class WeatherJson { //weatherinfo需要对应json数据的名称,我之前随便写了个,被坑很久 private Weatherinfo weatherinfo; public Weatherinfo getWeatherinfo() { return weatherinfo; } public void setWeatherinfo(Weatherinfo weatherinfo) { this.weatherinfo = weatherinfo; } //city、cityid必须对应json数据的名称,不然解析不了 public class Weatherinfo { private String city; private String cityid; private String temp; private String WD; private String WS; private String SD; private String WSE; private String time; private String isRadar; private String Radar; private String njd; private String qy; //这里省略get和和set方法 }}
修改Interface里的ResponseBody为WeatherJson
public interface TestGetService { @GET("adat/sk/{cityId}.html") Call getWeather(@Path("cityId") String cityId);}
添加addConverterFactory(GsonConverterFactory.create())
Retrofit retrofit = new Retrofit.Builder() //这里建议:- Base URL: 总是以/结尾;- @Url: 不要以/开头 .baseUrl("http://www.weather.com.cn/") .addConverterFactory(GsonConverterFactory.create()) .build(); TestGetService apiStores = retrofit.create(TestGetService.class); Call call = apiStores.getWeather("101010100"); call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { Log.e("xue", "getWeatherinfo=" + response.body().getWeatherinfo().getCity()); } @Override public void onFailure(Call call, Throwable t) { } });
(5)RxJava
修改interface为
public interface TestGetService { @GET("adat/sk/{cityId}.html") Observable getWeather(@Path("cityId") String cityId);}
增加.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
代码修改为
Retrofit retrofit = new Retrofit.Builder() //这里建议:- Base URL: 总是以/结尾;- @Url: 不要以/开头 .baseUrl("http://www.weather.com.cn/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); TestGetService apiStores = retrofit.create(TestGetService.class); Observable observable = apiStores.getWeather("101010100"); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(WeatherJson weatherJson) { Log.e("mylog", weatherJson.getWeatherinfo().getCity()); } });
总结:
这里是小试一下,没有做封装,封装可参考https://github.com/Tamicer/Novate
感觉代码比其他框架变多了,优点就是rxjava的优点
参考
http://blog.csdn.net/liangdong131/article/details/51791034
更多相关文章
- Android中使用gradient的一条建议
- 给android新手的10条建议
- 30+关于如何成为更好Android开发者的短小而专业的建议
- cvc-complex-type.2.4.d: 发现了以元素 'd:skin' 开头的无效内容
- 关于使用 Android MVVM + LiveData 模式的一些建议
- Android代码内存优化建议-Android资源篇
- android 建议在onPause和onStop处理的事情
- 给学android的同学们的一些建议【III】
- Android最佳实践之Notification、下拉刷新、内存及性能建议等