前言
上周我写了一篇MPAndroidChart的使用技巧 ,得到了不少人的响应。至少自己写的文章还是有人去看,很是激励。毕竟我在学习Android开发的时候也是一直看着同行前辈们在技术社区上的干货输出,慢慢地进步起来。当在一些技术点上有了自己的一般见解,也应当回馈社区。
感概完了,现在还是言归正传吧。当掌握了一个技术点之后,怎样在项目中运用到这个技术点,还需要自己好好琢磨琢磨一下的。上次在MPAndroidChart的使用技巧案例中,数据都是自己写死的。现在就来看看MPAndroidChart怎样结合后台的传过来的数据展示图表吧。
在这次的案例中,我使用的是近段时间以来很潮很火的Retrofit+RxJava(这两种开发技术堪称一对CP啊!)来对后台进行网络请求。如果对Retrofit使用还不是很熟悉的话,可以看看之前我的写过一篇 Retrofit网络请求框架基础操作。而对于RxJava,我并未进行深入的研究,但可以看看一些大神写的博客,例如这位大头鬼Bruce。(PS:在案例中我对Retrofit请求时做了一些封装,在点击button前,未显示图表,点击button之后,才显示图表)
Talk is cheap,show you the code
导入MPAndroidChart的jar包(这一步可以参考MPAndroidChart的使用技巧)
添加依赖
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.0'
compile 'com.orhanobut:logger:1.11'
compile project(':JNChartLib')
- 创建请求接口(使用的接口地址:http://apis.baidu.com/heweather/weather/free )
public interface NetRequest {