Android-网络框架04Retrofit2.0+RxJava
16lz
2022-06-04
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怎样添加AT命令
- android实现图片平铺效果&WebView多点触控实现缩放
- 如何添加Android返回键的退出功能
- Android(安卓)init.rc BOOTCLASSPATH
- 通过AndroidTestCase来进行android 单元测试
- android APP 获得system权限
- 2011.09.13(3)——— android 添加快捷方式并且图标上添加数字
- 去掉所有Activity界面标题栏、强制横屏竖屏
- android 完美退出所有Activity的demo