用到的资源库
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
compile 'org.jetbrains.anko:anko-sdk15:0.8.2'
compile 'org.jetbrains.anko:anko-support-v4:0.8.2'
compile 'org.jetbrains.anko:anko-appcompat-v7:0.8.2'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.github.bumptech.glide:glide:4.0.0-RC0'
实体类
package demo.kotlin.rxretrofit
/**
* Created by gjl on 2017/5/18.
*/
data class MovieEntity(
var count: Int = 0,
var start: Int = 0,
var total: Int = 0,
var title: String? = null,
var subjects: List? = null) {
data class SubjectsBean(
var rating: RatingBean? = null,
var title: String? = null,
var collect_count: Int = 0,
var original_title: String? = null,
var subtype: String? = null,
var year: String? = null,
var images: ImagesBean? = null,
var alt: String? = null,
var id: String? = null,
var genres: List? = null,
var casts: List? = null,
var directors: List? = null) {
data class RatingBean(
private var max: Int = 0,
private var average: Double = 0.0,
private var stars: String? = null,
private var min: Int = 0) {}
data class ImagesBean(
var small: String? = null,
var large: String? = null,
var medium: String? = null) {}
data class DirectorsBean(
var alt: String? = null,
var avatars: AvatarsBean? = null,
var name: String? = null,
var id: String? = null) {}
data class CastsBean(
var alt: String? = null,
var avatars: AvatarsBean? = null,
var name: String? = null,
var id: String? = null) {}
data class AvatarsBean(
var small: String? = null,
var large: String? = null,
var medium: String? = null) {}
}
}
activity
class Main3Activity : AppCompatActivity() {
var text_view: TextView? = null
var my_adpter: MyAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Main3ActivityUI(this).setContentView(this)
}
class Main3ActivityUI(var activity: Main3Activity) : AnkoComponent {
override fun createView(ui: AnkoContext) = with(ui) {
var list = ArrayList()
activity.my_adpter = MyAdapter(list)
verticalLayout {
fitsSystemWindows = true
activity.text_view = textView("标题") {
textColor = Color.BLUE
textSize = 25f
background = ColorDrawable(Color.CYAN)
paddingHorizontal = 35
gravity = Gravity.CENTER
}.lparams(matchParent, wrapContent)
listView() {
adapter = activity.my_adpter
onItemClick { adapterView, view, i, l -> show(i) }
}.lparams(matchParent, matchParent) {
weight = 1f
}
button("点我") {
textColor = Color.RED
textSize = 20F
onClick { activity.getMovie() }
}.lparams(wrapContent, wrapContent) {
gravity = Gravity.CENTER
}
}
}
fun show(i:Int){
val subjectsBean = activity.my_adpter?.getItem(i)
activity.toast(subjectsBean?.original_title.toString())
}
}
fun getMovie() {
text_view?.text = "正在请求,请稍后..."
HttpMethods3.getInstance().getTopMovie1(object : Subscriber() {
override fun onNext(t: MovieEntity?) {
if (t != null) {
text_view?.text = t.title
my_adpter?.list = t.subjects!!
my_adpter?.notifyDataSetChanged()
}
}
override fun onCompleted() {
}
override fun onError(e: Throwable?) {
text_view?.text = "请求失败,请重试"
}
}!!, 0, 10)
}
class MyAdapter(var list: List) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val subjectsBean = getItem(position)
val inflate = View.inflate(parent?.context, R.layout.layout_item, null)
Glide.with(parent).load(subjectsBean.images?.small).into(inflate.find(R.id.iv_icon))
inflate.find(R.id.tv_title).text = subjectsBean.title+"\n"+subjectsBean.year
return inflate
}
override fun getItem(position: Int) = list.get(position)
override fun getItemId(position: Int) = position.toLong()
override fun getCount() = list.size
}
}
网络请求
class HttpMethods3{
val BASE_URL = "https://api.douban.com/v2/movie/"
var retrofit: Retrofit?=null
var movieService: MovieService?=null
private constructor() {
var httpClientBuilder: OkHttpClient.Builder = OkHttpClient.Builder()
retrofit = Retrofit.Builder()
.client(httpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(BASE_URL)
.build()
movieService = retrofit?.create(MovieService::class.java)
}
companion object{
fun getInstance() = Holder.instance
}
private object Holder{
val instance = HttpMethods3()
}
/**
* 用于获取豆瓣电影Top250的数据
* @param subscriber 由调用者传过来的观察者对象
* @param start 起始位置
* @param count 获取长 */
fun getTopMovie1(subscriber: Subscriber, start:Int, count:Int){
movieService?.getTopMovie(start, count)
?.subscribeOn(Schedulers.io())
?.unsubscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(subscriber)
}
}
==============================================================================
/**
* Created by gjl on 2017/5/18.
*/
interface MovieService {
@GET("top250")
abstract fun getTopMovie(@Query("start") start: Int, @Query("count") count: Int): Observable
}
条目布局