retrofit和RxJava结合
2020-12-13 01:55
标签:thread obs 直接 res tco ret err save let 主方法 新建类 直接把依赖复制到build.gride然后同步 retrofit和RxJava结合 标签:thread obs 直接 res tco ret err save let 原文地址:https://www.cnblogs.com/Ocean123123/p/11018818.htmlpublic class MainActivity extends AppCompatActivity {
@SuppressLint("CheckResult")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiClient.retrofit().create(ApiStores.class).loadDataByRetrofitRxJava("101220602")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer
public class ApiClient {
public static Retrofit mRetrofit;
public static Retrofit retrofit() {
if (mRetrofit == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.readTimeout(5, TimeUnit.SECONDS);
builder.connectTimeout(5, TimeUnit.SECONDS);
if (BuildConfig.DEBUG) {
// Log信息拦截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//设置 Debug Log 模式
builder.addInterceptor(loggingInterceptor);
}
OkHttpClient okHttpClient = builder.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(MainActivity.ApiStores.API_SERVER_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
}
return mRetrofit;
}
}
public class MainModel {
public static WeatherinfoBean getWeatherinfo() {
return new WeatherinfoBean();
}
public static class WeatherinfoBean {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
}
implementation ‘io.reactivex.rxjava2:rxjava:2.2.9‘
implementation ‘io.reactivex.rxjava2:rxandroid:2.1.1‘
//retrofit
implementation ‘com.squareup.retrofit2:retrofit:2.6.0‘
//Gson converter
implementation ‘com.squareup.retrofit2:converter-gson:2.6.0‘
//RxJava2 Adapter
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
//okhttp
implementation ‘com.squareup.okhttp3:okhttp:3.12.0‘
implementation ‘com.squareup.okhttp3:logging-interceptor:3.9.1‘
上一篇:隐藏web服务的头信息