前几天分别对Retrofit和RxJava进行了总结,这个帖子打算把Retrofit结合RxJava使用的方法总结以下。有还不了解Retrofit或RxJava的朋友可以参考下面的帖子学习~
首先导入依赖:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'io.reactivex:rxjava:1.2.2'
compile 'io.reactivex:rxandroid:1.2.1'
}
<uses-permission android:name="android.permission.INTERNET" />
在贴出代码之前先声明一下,这个帖子里面用到的案例和Retrofit的帖子中用到的案例是同一个。
代码:
两者结合的代码(未封装):
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SharedData.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RetrofitService service = retrofit.create(RetrofitService.class);
Observable<InfoData> observable = service.getInfoData();
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<InfoData>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(InfoData infoData) {
Toast.makeText(MainActivity.this, infoData.getName(), Toast.LENGTH_SHORT).show();
}
});
两者结合的代码(封装):
封装好的工具类:
import com.example.itgungnir.testretrofit_rxjava.share.SharedData;在主线程中调用工具类:
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* 网络访问的工具类
*/
public class HttpUtil {
private static HttpUtil instance;
private Retrofit retrofit;
private HttpUtil() {
this.instance = this;
this.retrofit = new Retrofit.Builder()
.baseUrl(SharedData.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
private static HttpUtil getInstance() {
if (instance == null) {
synchronized (HttpUtil.class) {
if (instance == null) {
return new HttpUtil();
}
}
}
return instance;
}
public static <T> T getService(Class<T> c) {
return getInstance().retrofit.create(c);
}
public static <T> void init(Observable<T> observable, Subscriber<T> subscriber) {
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpUtil.init(HttpUtil.getService(RetrofitService.class).getInfoData(), new Subscriber<InfoData>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(InfoData infoData) {
Toast.makeText(MainActivity.this, infoData.getName(), Toast.LENGTH_SHORT).show();
}
});
}
}
运行结果: