/** * APIKEY=‘71e58b5b2f930eaf1f937407acde08fe’ * http://api.tianapi.com/nba/?key=APIKEY&num=10*/
compile 'io.reactivex:rxjava:1.3.4'compile 'io.reactivex:rxandroid:1.2.1'compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0-beta4'compile 'com.squareup.okhttp3:okhttp:3.9.0'
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
public class RetrofitUtils {private static volatile RetrofitUtils instance;private Retrofit retrofit;private RetrofitUtils() {}private RetrofitUtils(String baseUrl) {OkHttpClient client = new OkHttpClient();retrofit = new Retrofit.Builder().client(client).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).baseUrl(baseUrl).build();}public static RetrofitUtils getInstance(String baseUrl) {if (instance == null) {synchronized (RetrofitUtils.class) {if (null == instance) {instance = new RetrofitUtils(baseUrl);}}}return instance;}public static RetrofitUtils getInstance() {if (null == instance) {/*** APIKEY=‘71e58b5b2f930eaf1f937407acde08fe’* http://api.tianapi.com/nba/?key=APIKEY&num=10*/return getInstance("http://api.tianapi.com/");}return instance;}public Retrofit getRetrofit() {return retrofit;} }
public interface BlogService {@GET("nba/")Observable<JavaBean> getUrl(@Query("key") String key, @Query("num") int num);}
public interface NewsView {void success(List<JavaBean.NewslistBean> data);void failed(String e); }
public class NewsPresenter {private NewsView inv;private Subscription subscribe;public void attachView(NewsView inv){this.inv = inv;}public void getNews(String key,int num){Retrofit retrofit = RetrofitUtils.getInstance().getRetrofit();BlogService service = retrofit.create(BlogService.class);subscribe = service.getUrl(key, num).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<JavaBean>() {@Overridepublic void call(JavaBean javaBean) {List<JavaBean.NewslistBean> list = javaBean.getNewslist();inv.success(list);}}, new Action1<Throwable>() {@Overridepublic void call(Throwable throwable) {inv.failed(throwable.getMessage());}});}public void detachView(){// 当Activity销毁的时候取消订阅时间,防止内存泄漏if (subscribe != null) {if (subscribe.isUnsubscribed()) {subscribe.unsubscribe();}}if (inv!=null){inv = null;}} }
public class MainActivity extends AppCompatActivity {private List<JavaBean.NewslistBean> list = new ArrayList<>();private NewsPresenter presenter;private MyAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getData(10);}public void getData(int count) {presenter = new NewsPresenter();presenter.getNews("71e58b5b2f930eaf1f937407acde08fe", count);presenter.attachView(new NewsView() {@Overridepublic void success(List<JavaBean.NewslistBean> data) {list.clear();list.addAll(data);// if (adapter == null) { // adapter = new MyAdapter(list, MainActivity.this); // } else { // adapter.notifyDataSetChanged(); // }}@Overridepublic void failed(String e) {}});} }
详细解决方案
Retrofit+Rxjava 等
热度:51 发布时间:2023-11-15 19:15:42.0
相关解决方案
- 51.Rxjava+RxAndroid+Retrofit组合有关问题
- RxJava - 当另一个完成时启动一个Observable
- 仅在其他方法完成后才调用方法-RxJava
- RxJava 中 AndroidSchedulers.mainThread() 的替代方法是什么?
- 如何在 Retrofit (Android) 中没有任何键的情况下获取 JSON 数组?
- RxJava flatMapIterable与concatMap
- Android Retrofit + Realm + Gson:未调用序列化器
- kotlin--综合运用Hilt、Paging3、Flow、Room、Retrofit、Coil等实现MVVM架构实战
- 轻量级kotlin + Mvp + Rxjava + Retrofit框架
- RxJava 2.X几个简单的操作符释义
- RxJava 1.0版本入门篇之--4
- RxJava 1.0版本入门篇之--3
- RxJava 1.0版本入门篇之--2
- RxJava 1.0版本入门篇之--1
- RxJava+Retrofit
- RxJava 学习使用心得
- RxJava 嵌套调用接口
- RxJava 在Android开发上的应用
- Android MVP+Retrofit(封装)+RxJava实例
- android 封装Retrofit+RxJava
- RxJava 2.x 学习
- Retrofit+Rxjava 等
- Android-RxJava(下)
- Android-RxJava(上)
- 根据城市名称检索城市ID,以及省市县(LitePal+RxJava+Retrofit)
- Dagger2+Retrofit+RxJava
- RxJava+Retrofit通用封装
- Retrofit 设置 超时时间
- okhttp 和 retrofit 源码解析系列导读
- Rxjava Subject分析