当前位置: 代码迷 >> 综合 >> Retrofit+Rxjava 等
  详细解决方案

Retrofit+Rxjava 等

热度:51   发布时间:2023-11-15 19:15:42.0
/**  * 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) {}});}
}