Kotlin解串

RetrofitManager
object RetrofitManager {

    fun getRetrofit():Retrofit{
        var cline = OkHttpClient().newBuilder()
            .writeTimeout(30,TimeUnit.SECONDS)
            .readTimeout(30,TimeUnit.SECONDS)
            .connectTimeout(30,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
        return Retrofit.Builder()
            .client(cline)
            .baseUrl(Url)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }
}
Response
data class Resp<T>(var data:T,var code:Int,var message:String)
Entity
data class GoodsEntity(
    val bannerList: List<String>,
    val category_id: Int,
    val goods_attribute: String,
    val goods_banner: String,
    val goods_code: String,
    val goods_default_icon: String,
    val goods_default_price: Int,
    val goods_desc: String,
    val goods_detail_one: String,
    val goods_detail_two: String,
    val goods_sales_count: Int,
    val goods_stock_count: Int,
    val id: Int
)
Adapter
class GoodsAdapter(aa:Int):BaseQuickAdapter<"实体类",BaseViewHolder>(aa) {
    override fun convert(holder: BaseViewHolder, item: "实体类") {
        val imageView = holder.getView<ImageView>("ImageView的ID")
        Glide.with(context).load("路径").into(imageView)
        holder.setText("TextView的ID",item.goods_desc)
    }
}
ApiServers

interface ApiServers {
    @GET(Url)
    suspend fun getGoods(@Query("接口名字")"参数名" :"参数类型"):ResPonse<MutableList<"实体类">>
}
MainActivity
class MainActivity : AppCompatActivity() {
    //适配器
    lateinit var goodsAdapter: GoodsAdapter
    //视图控件
    private lateinit var dataBinding:ActivityMainBinding
    //线程
    private var dataJob = Job()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //绑定视图控件
        dataBinding = DataBindingUtil.setContentView(this,R.layout.activity_main)
        initView()
        initData()
    }

    private fun initData() {
        //协程
        CoroutineScope(dataJob).launch(Dispatchers.Main) {
            //解串
            var aaa :Resp<MutableList<GoodsEntity>> = RetrofitManager.getRetrofit().create(ApiServers::class.java).getGoods(0,1,20)
            if (aaa.code==200){
                //串塞进去
                goodsAdapter.data.addAll(aaa.data)
                //刷新适配器
                goodsAdapter.notifyDataSetChanged()
            }
        }
        val map: MutableMap<String, String> = HashMap()
            map["username"] = username.toString()
            map["password"] = password.toString()
            val create:RequestBody = RequestBody.create("application/json;charset=utf-8".toMediaType(),GsonUtils.toJson(map))
            CoroutineScope(dataJob).launch(Dispatchers.Main) {
                var data1: Response<LoginEntity> = RetrofitManager.getRetrofit().create(ApiServers::class.java).getLogin(create)
                if (data1.code==200){
                    Toast.makeText(baseContext,"登录成功",Toast.LENGTH_SHORT).show()
                    ARouter.getInstance().build("/app/MainActivity").navigation()
                }
            }
    }

    private fun initView() {
        //初始化适配器
        goodsAdapter = GoodsAdapter(R.layout.goodsitem)
        //设置适配器
        dataBinding.rv.adapter = goodsAdapter
        //设置样式
        dataBinding.rv.layoutManager = GridLayoutManager(this,2)
    }
}
页面布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="name"
            type="com.example.lx.GoodsEntity" />
    </data>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_gravity="center"
            android:id="@+id/goods_iv"
            android:layout_width="150dp"
            android:layout_height="150dp"/>
        <TextView
            android:id="@+id/goods_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</layout>
build.gradle(Project:lx)
plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(Model:lx.app)

plugins {
    id 'kotlin-kapt'
}
android {
    compileSdk 33
    dataBinding{

        enabled=true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
dependencies {
     //ARouter依赖
    implementation 'com.alibaba:arouter-api:1.5.2'
    implementation 'androidx.navigation:navigation-fragment:2.5.2'
    implementation 'androidx.navigation:navigation-ui:2.5.2'
    annotationProcessor 'com.alibaba:arouter-compiler:1.5.2'

    //环信
    implementation 'io.hyphenate:hyphenate-chat:3.9.4'

    //分包
    implementation 'com.android.support:multidex:1.0.3'

    //底部导航
    implementation 'com.ashokvarma.android:bottom-navigation-bar:2.2.0'
    implementation 'com.youth.banner:banner:1.4.10'//轮播图
    implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.0.2@aar'//flycotablayout

    implementation 'com.android.support:recyclerview-v7:33.0.0'//recyclerview


    implementation 'top.littlefogcat.easydanmaku:easydanmaku:0.1.3'//简单弹幕
    implementation 'com.github.kaisengao:KsgLikeView:1.2.2'//点赞
    implementation 'com.github.ctiao:DanmakuFlameMaster:0.9.25'//烈焰弹幕

    implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-java:v8.3.5-release-jitpack'

    //是否需要ExoPlayer模式
    implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer-exo2:v8.3.5-release-jitpack'

    //是否需要AliPlayer模式
    implementation 'com.github.CarGuo.GSYVideoPlayer:GSYVideoPlayer-aliplay:v8.3.5-release-jitpack'

    //根据你的需求ijk模式的so
    implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-arm64:v8.3.5-release-jitpack'
    implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-armv7a:v8.3.5-release-jitpack'
    implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-armv5:v8.3.5-release-jitpack'
    implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-x86:v8.3.5-release-jitpack'
    implementation 'com.github.CarGuo.GSYVideoPlayer:gsyVideoPlayer-x64:v8.3.5-release-jitpack'
    // 工具类
    implementation 'com.blankj:utilcodex:1.30.6'

    // 万能适配器
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.0'

    // okhttp
    implementation 'com.squareup.okhttp3:okhttp:4.2.2'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

    //glide
    implementation 'com.github.bumptech.glide:glide:4.15.1'
    //毛玻璃效果
    implementation 'jp.wasabeef:glide-transformations:4.3.0'

    // retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'

    //GreenDao数据库
    implementation 'org.greenrobot:greendao:3.3.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'

    //沉浸式状态栏
    implementation 'com.jaeger.statusbarutil:library:1.4.0'
    implementation 'org.greenrobot:eventbus:3.3.1'

    //Rx
    implementation 'io.reactivex.rxjava2:rxjava:2.2.7'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
}
gradle-wrapper.properties(Gradle Version)
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.3.3-bin.zip
gradle.properties (Project Properties)
android.enableJetifier=true
settings.gradle (Project Settings)
pluginManagement {
    repositories {
        // 阿里镜像源
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }

        jcenter()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        // 阿里镜像源
        maven { url 'https://maven.aliyun.com/repository/central' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url "https://jitpack.io" }

        jcenter()
        mavenCentral()
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值