安卓Retrofit发送单个或多个网络请求(串行)

目标

采用Retrofit,发送单个或多个网络请求(串行),并更新UI.

实现方案

发送单个网络请求方案

  1. 修改app工程的build.gradle文件
  • 在android节点下添加如下信息
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
  • 添加依赖
implementation 'com.squareup.retrofit2:retrofit:2.7.2'
implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
  1. 定义数据类Task.kt
data class Task(val id: Int, var name: String)
  1. 创建网络接口类NetworkService.kt
interface NetworkService {
    @GET("cxyzy1/AndroidAsyncTaskDemo/raw/master/data.json")
    fun query(): Call<Task>
}
  1. 创建网络工具类NetworkUtils.kt
fun getNetworkService(): NetworkService {
    val okHttpClient = OkHttpClient.Builder().build()
    val retrofit = Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl("https://gitee.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    return retrofit.create(NetworkService::class.java)
}
  1. activity调用
private fun queryData() {
    val networkService = getNetworkService()
    val call = networkService.query()
    call.enqueue(object : Callback<Task?> {
        override fun onResponse(call: Call<Task?>?, response: Response<Task?>) {
            val bean: Task? = response.body()
            contentTv.text = bean?.name
        }

        override fun onFailure(call: Call<Task?>?, t: Throwable) {
            Log.e("MainActivity", "网络请求失败:" + t.message)
        }
    })
}

发送两个网络请求方案

  1. 修改app工程的build.gradle文件,同上
  2. 定义数据类Task.kt,同上
  3. 创建网络接口类NetworkService.kt
interface NetworkService {
    @GET("cxyzy1/AndroidAsyncTaskDemo/raw/master/data.json")
    fun query(): Call<Task>

    @GET("cxyzy1/AndroidAsyncTaskDemo/raw/master/data2.json")
    fun query2():  Call<Task>
}
  1. 创建网络工具类NetworkUtils.kt,同上
  2. activity调用
private fun queryData() {
    val networkService = getNetworkService()
    val call = networkService.query()
    call.enqueue(object : Callback<Task?> {
        override fun onResponse(call: Call<Task?>?, response: Response<Task?>) {
            val bean: Task? = response.body()
            contentTv.text = bean?.name
            queryData2()
        }

        override fun onFailure(call: Call<Task?>?, t: Throwable) {
            Log.e("MainActivity", "网络请求失败:" + t.message)
        }
    })
}

private fun queryData2() {
    val networkService = getNetworkService()
    val call = networkService.query2()
    call.enqueue(object : Callback<Task?> {
        override fun onResponse(call: Call<Task?>?, response: Response<Task?>) {
            val bean: Task? = response.body()
            contentTv2.text = bean?.name
        }

        override fun onFailure(call: Call<Task?>?, t: Throwable) {
            Log.e("MainActivity", "网络请求失败:" + t.message)
        }
    })
}

代码说明:
queryData访问query接口完毕后,通过调用queryData2访问query2接口.

完整源代码

https://gitee.com/cxyzy1/AndroidAsyncTaskDemo/tree/master/retrofitDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值