Android简单使用retrofit实现左右联动

本文档详细介绍了如何在Android应用中使用Retrofit库实现左右联动的效果。内容包括依赖配置、实体类创建、ApiService接口定义、Retrofit工具类、MVP模式的Model、View、Presenter层的实现,以及左右侧适配器的配置。适用于Android初学者学习。

效果图

在这里插入图片描述

所需依赖

依赖较多

implementation ‘com.jakewharton:butterknife:10.2.1’//Butterknife配置
annotationProcessor ‘com.jakewharton:butterknife-compiler:10.2.1’//Butterknife注解处理器
implementation ‘com.github.bumptech.glide:glide:4.10.0’//图片加载框架

implementation 'androidx.recyclerview:recyclerview:1.1.0'//recycerview


implementation 'org.greenrobot:eventbus:3.1.1' //eventbus 事件总线,传值
//retrofit核心库
implementation 'com.squareup.retrofit2:retrofit:2.6.2'

//retrofit辅助,gson解析的库
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.2' // 必要依赖,和Rxjava结合必须用到,下面会提到
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'//切换到主线程的依赖

//okhttp日志拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'

首先先创建左右侧的Entity

创建ApiService接口

public interface ApiService {
@GET(“左侧尾部地址”)
Observable < LeftEntity > getResult();
//Observable导io包,左侧Entity的名称及获取的集合名称
@GET(“右侧尾部地址”)
Observable< RightEntity > getRight(@QueryMap HashMap<String,String> params);
//右侧Entity的名称及获取的集合名称(获取map,创建Hasmap)
}

创建RetrofitUtils工具类

public class RetroitUtils {
private static RetroitUtils retroit;
private final Retrofit retrofit;

public RetroitUtils() {
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build();

    retrofit = new Retrofit.Builder()
            .baseUrl("左右侧前段相同地址")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
}

public static RetroitUtils getInstance() {
    if (retroit==null){
        synchronized (RetroitUtils.class){
            if (retroit==null){
                retroit=new RetroitUtils();
            }
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值