依赖:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'com.github.bumptech.glide:glide:3.7.0'
布局
主布局activity_main
bean类
package com.example.muhanxi.kotlindemo
/**
* Created by muhanxi on 17/12/14.
*/
data class Bean(
val error_code: Int, //200
val reason: String, //请求成功!
val result: Result
)
data class Result(
val total: Int, //15767
val limit: Int, //20
val bookList: List
)
data class Book(
val name: String, //灵神考试
val type: String, //少年漫画
val area: String, //国漫
val des: String,
val finish: Boolean, //false
val lastUpdate: Int, //20150603
val coverImg: String //http://imgs.juheapi.com/comic_xin/5559b86938f275fd560ad613.jpg
)
数据接口IGetDataBase
package com.example.muhanxi.kotlindemo
import io.reactivex.Observable
import okhttp3.ResponseBody
import retrofit2.http.GET
/**
* Created by muhanxi on 17/12/13.
*/
interface IService {
@GET("/comic/book?key=f54c4c57143b8fad9bf3193cab52a81c")
fun getData() : Observable
}
MainActivity主类
package com.example.muhanxi.kotlindemo
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Toast
import com.google.gson.Gson
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.internal.util.BackpressureHelper.add
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_main.*
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
class MainActivity : Activity() {
lateinit var adapter : IAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerview.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
adapter = IAdapter(this)
recyclerview.adapter =adapter
getData()
}
fun getData(){
var retrofit = Retrofit.Builder()
.baseUrl("http://japi.juhe.cn")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
var iservice : IService = retrofit.create(IService::class.java)
iservice.getData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe( {
next ->
var result = next.string()
println(result)
var gson = Gson()
var bean = gson.fromJson(result,Bean::class.java)
adapter.addData(bean)
} , {
t ->
} )
// iservice.getData()
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe({
// next ->
//
// // onnext
//
// },{
// t ->
//// error
// },{
//// oncomplete
// },{
// d ->
//// d.dispose()
// })
}
}
适配器IAdapter
package com.example.muhanxi.kotlindemo
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
/**
* Created by muhanxi on 17/12/14.
*/
class IAdapter(context1 : Context) : RecyclerView.Adapter() {
var context : Context = context1
var list : ArrayList = ArrayList()
fun addData(bean : Bean) {
list.addAll(bean.result.bookList)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): IViewHodler {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
var view = LayoutInflater.from(context).inflate(R.layout.layout,parent,false)
return IViewHodler(view)
}
override fun onBindViewHolder(holder: IViewHodler?, position: Int) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
holder!!.item_textview.setText(list.get(position).name)
Glide.with(context).load(list.get(position).coverImg).into(holder!!.item_imageview)
}
override fun getItemCount(): Int {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return list.size
}
class IViewHodler(view : View) : RecyclerView.ViewHolder(view) {
lateinit var item_imageview : ImageView
lateinit var item_textview : TextView
init {
item_imageview = view.findViewById(R.id.item_imageview)
item_textview = view.findViewById(R.id.item_textview)
}
// constructor(view : View) : super(view) {
// item_imageview = view.findViewById(R.id.item_imageview)
// item_textview = view.findViewById(R.id.item_textview)
// }
}
}