android – 如何使用RxJava和Kotlin从列表中获取Arraylist中的Json Response
作者:互联网
面对在ArrayList中获取响应的问题.
我有以下Respose on String值
var res_message: String = ""
res_message = "${result.vehicletypes} "
低于此字符串的值
[VehicleType(_id=1, vehicleType=Hatchback, __v=0),
VehicleType(_id=2, vehicleType=Maruti, __v=0),
VehicleType(_id=3, vehicleType=Honda, __v=0),
VehicleType(_id=4, vehicleType=Bike, __v=0)]
改造结果是
vehicletypes = {ArrayList@6055} size = 4
0 = {Model$VehicleType@6058} "VehicleType(_id=1,
vehicleType=Hatchback, __v=0)"
1 = {Model$VehicleType@6059} "VehicleType(_id=2,
vehicleType=Maruti, __v=0)"
2 = {Model$VehicleType@6060} "VehicleType(_id=3,
vehicleType=Honda, __v=0)"
3 = {Model$VehicleType@6061} "VehicleType(_id=4,
vehicleType=Bike, __v=0)"
下面的代码snippest向API发送请求.
disposable = apiServices.getVehicle(token)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ result ->
res_message = "${result.vehicletypes} "
Log.d("Type==", res_message)
},
{ error ->
res_message = "${error.message}"
// validateToken()
}
)
模型类
data class Vehicles(val success: Boolean, val vehicletypes: List<VehicleType>, val message: String)
data class VehicleType(val _id: String, val vehicleType: String, val __v: String)
我想在下面的vehicleListArray上的Arralist VehicleType List上获得这个值
private var vehicleListArray: ArrayList<Model.VehicleType>? = null
我们如何实现这一目标.
提前致谢.
解决方法:
假设您要解析的是来自服务的响应,该服务能够向您发送列表的适当格式(例如Json)而不是
Retrofit可以轻松处理解析列表.
在您的apiService定义中:
fun getPeople(token: Whatever): Observable<List<VehicleType>>
如果你还没有:
Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
标签:android,retrofit2,kotlin,rx-kotlin 来源: https://codeday.me/bug/20190705/1389051.html