赞
踩
(1). 在build.gradle引入retrofit2和rxjava3
//rxjava3
api 'io.reactivex.rxjava3:rxandroid:3.0.0'
api 'io.reactivex.rxjava3:rxjava:3.0.0'
//retrofit
api 'com.squareup.retrofit2:converter-gson:2.9.0'
api 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
(2). 创建要访问网络接口
(3).构建Retrofit,通过Hilt注解方式来获取网络接口动态代理类,通过代理类获取网络数据,通过Rxjava3接收数据流
@Provides
fun provideService(retrofit:Retrofit):ApiService =
retrofit.create(ApiService::class.java)
@Singleton
@Provides
fun provideRetrofit(okhttpClient:OkHttpClient):Retrofit {
return Retrofit.Builder()
.client(okhttpClient)
.baseUrl(Constants.API_URL)
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Provides
fun provideClient():OkHttpClient {
return OkHttpClient.Builder()
.readTimeout(60,TimeUnit.SECONDS)
.writeTimeout(60,TimeUnit.SECONDS)
.connectTimeout(60,TimeUnit.SECONDS)
.build()
}
通过hilt注解获取apiService
(4)将获取到的ApiService代理类注入到repository中
注入到repository中,就可以愉快地使用apiService来访问网络接口啦,例如
fun checkAppUpdate():Maybe {
return apiService.checkAppUpdate()
}
这样就将网络接口封装到了repository仓库中啦,利用Hilt将Repository注解为单例,这样就可以在要是使用的activity注入使用了,例如:
hilt单例注解
注入repository仓库
(1)引入room相关依赖
def room_version ="2.2.5"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
(2)构建room实体类和操作接口dao
@Entity(tableName = "push_msg")
data class PushMsg (
@PrimaryKey(autoGenerate = true)
var id:Long,
@ColumnInfo(name = "str1")
var str1:String)
实体类
@Dao
interface PushMsgDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAllData(list:MutableList)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertMsg(element:PushMsg)
@Query("select * from push_msg")
fun getAllMsg():MutableList
@Query("select * from push_msg where id = :id")
fun getMsgById(id:Long):MutableList
@Delete
fun deleteMsg(element:PushMsg)
}
操作接口
(3)创建room数据管理类AppDateBase
@Database(entities = {
UserInfo.class,
PushMessage.class,
PushMsg.class
},version =1,exportSchema =false)
public abstract class AppDataBase extends RoomDatabase {
public abstract UserDao userDao();
public abstract PushMessageDao pushMessageDao();
public abstract PushMsgDao pushMsgDao();
}
database
(4)通过hilt获取AppDataBase
(5)将AppDataBase注入到repository中统一管理
通过注入的localDb对数据库进行操作,数据库操作是耗时IO操作,不能在主线程直接调用该方法,可通过协程进行相关调用。
在buildgradle中引入协程依赖
//协程
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
/**
* 将数据存入room数据库
*/
fun insertPushMsg(msg:PushMsg) {
localDb.pushMsgDao().insertMsg(msg)
}
/**
* 获取数据
*/
fun getAllMsg():MutableList {
return localDb.pushMsgDao().getAllMsg()
}
声明协程作用域,在onDestroy需要cancel掉
存入数据
整体结构目录,appModule提供相应Provides方法
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。