RxJava如何与Retrofit结合
先扔出build.gradle文件的内容
dependencies { compile fileTree(dir: \'libs\', include: [\'*.jar\']) testCompile \'junit:junit:4.12\' compile \'com.android.support:appcompat-v7:23.2.0\' compile \'io.reactivex:rxjava:1.1.0\' compile \'io.reactivex:rxandroid:1.1.0\' compile \'com.squareup.retrofit2:retrofit:2.0.0-beta4\' compile \'com.squareup.retrofit2:converter-gson:2.0.0-beta4\' compile \'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4\' compile \'com.google.code.gson:gson:2.6.2\' compile \'com.jakewharton:butterknife:7.0.1\' }
也就是说本文是基于RxJava1.1.0和Retrofit 2.0.0-beta4来进行的。 添加rxandroid是因为rxjava中的线程问题。
下面先搭建一个基本的页面,页面很简单,先来看文件目录结构
activity_main.xml的代码如下:
<?xml version=\"1.0\" encoding=\"utf-8\"?> <RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:paddingBottom=\"@dimen/activity_vertical_margin\" android:paddingLeft=\"@dimen/activity_horizontal_margin\" android:paddingRight=\"@dimen/activity_horizontal_margin\" android:paddingTop=\"@dimen/activity_vertical_margin\" tools:context=\".activity.MainActivity\"> <Button android:id=\"@+id/click_me_BN\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" android:layout_alignParentBottom=\"true\" android:padding=\"5dp\" android:text=\"点我\" android:textSize=\"16sp\"/> <TextView android:id=\"@+id/result_TV\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:layout_above=\"@id/click_me_BN\" android:text=\"Hello World!\" android:textSize=\"16sp\"/> </RelativeLayout>
MainActivity.java的代码如下:
package com.queen.rxjavaretrofitdemo.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.TextView; import com.queen.rxjavaretrofitdemo.R; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @Bind(R.id.click_me_BN) Button clickMeBN; @Bind(R.id.result_TV) TextView resultTV; @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(){ } }
注意不要忘记加网络权限
<uses-permission android:name=\"android.permission.INTERNET\"/>
缓存配置
app网络数据的离线缓存实现有很多种办法,例如存进数据库(保存json使用时拿出来解析),存专有文件,或SharedPreference等等,也可以自己实现LruCache和
DiskLruCache这两种缓存策略构成二级缓存(内存和磁盘)
缓存对于移动端是非常重要的存在:
okhttp的缓存设计和浏览器的缓存设计差不多,可以通过添加响应头的形式进行缓存处理。
retrofit是依赖okhttp的一套RESTful架构的Android(Java)客户端实现
通过构造retrofit时的.client()方法更改其中的okhttp的实现,从而达到缓存的效果,在这里不介绍retrofit的具体用法。
下面是一个例子实现了有网缓存,无网络只读取缓存
File cacheFile = new File(APP.getContext().getExternalCacheDir(),\"ZhiBookCache\"); Cache cache = new Cache(cacheFile,1024*1024*50); Interceptor interceptor = new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); if (!HttpUtils.isNetworkConnected(APP.getContext())) { request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } Response response = chain.proceed(request); if (HttpUtils.isNetworkConnected(APP.getContext())) { int maxAge = 0 * 60; // 有网络时 设置缓存超时时间0个小时 response.newBuilder() .header(\"Cache-Control\", \"public, max-age=\" + maxAge) .removeHeader(\"Pragma\")// 清除头信息,因为服务器如果不支持,会返回一些干扰信息,不清除下面无法生效 .build(); } else { // 无网络时,设置超时为4周 int maxStale = 60 * 60 * 24 * 28; response.newBuilder() .header(\"Cache-Control\", \"public, only-if-cached, max-stale=\" + maxStale) .removeHeader(\"Pragma\") .build(); } return response; } }; client = new OkHttpClient.Builder().cache(cache) .addInterceptor(interceptor) .build(); retrofit = new Retrofit.Builder() .baseUrl(RetrofitAPI.BASIC_DAILY) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build(); retrofitAPI = retrofit.create(RetrofitAPI.class);
之后调用下面代码获取请求后的序列化对象
retrofitAPI.getDaily() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<DailyBean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Log.d(TAG,e.getMessage()); } @Override public void onNext(DailyBean bean) { listener.onGetDailySuccess(bean); } });
最后附上retrofit接口部分
@GET(\"news/latest\") Observable<DailyBean> getDaily();
本文地址:https://www.stayed.cn/item/16045
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我