kotlin使用retrofit+rxjava
前言
本文主要为了跟风,顺便研究下kotlin怎么用
建立一个activity_main.xml
相关依赖导入
- 主工程目录下的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2.在程序所在的模块目录下build.gradle 下修改,主要是为了倒入依赖
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.recovery.kotlin"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.3.0'
compile 'com.google.code.gson:gson:2.8.1'
}
3.retrofit的接口引入
建立一个kotlin class,APIService.kt
package com.recovery.kotlin.request
import com.recovery.kotlin.model.WeatherData
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import rx.Observable
/**
* Created by tom on 2017/6/2.
*/
class APIService {
companion object {
val refrofit: Retrofit = Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(OkHttpClient.Builder().build())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://api.map.baidu.com/")
.build();
val Service:IAPIService= refrofit.create(IAPIService::class.java);
interface IAPIService {
@GET("telematics/v3/weather?output=json&ak=这里自己申请一个百度的key")
public fun getCityWeather(@Query("location") city: String): Observable
}
public fun getCityWeather(city: String): Observable {
return Service.getCityWeather(city)
}
}
}
WeatherData 数据类,这里的?符号需要关注一下
String?类型表示 是可空的string
String表示不可空的String ,
kotlin是空类型安全的
package com.recovery.kotlin.model
/**
* Created by tom on 2017/6/2.
*/
class WeatherData {
/**
* error : 0
* status : success
* date : 2017-06-02
* results : [{"currentCity":"杭州","pm25":"63","index":[{"title":"穿衣","zs":"热","tipt":"穿衣指数","des":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"感冒","zs":"少发","tipt":"感冒指数","des":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但因气温较高且风力较强,请适当降低运动强度并注意户外防风。"},{"title":"紫外线强度","zs":"中等","tipt":"紫外线强度指数","des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}],"weather_data":[{"date":"周五 06月02日 (实时:30℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东北风3-4级","temperature":"30 ~ 21℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"晴转多云","wind":"东北风3-4级","temperature":"30 ~ 20℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"东风3-4级","temperature":"26 ~ 19℃"},{"date":"周一","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"阵雨","wind":"东南风微风","temperature":"25 ~ 20℃"}]}]
*/
var error: Int = 0
var status: String? = null
var date: String? = null
var results: List? = null
class ResultsBean {
/**
* currentCity : 杭州
* pm25 : 63
* index : [{"title":"穿衣","zs":"热","tipt":"穿衣指数","des":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"感冒","zs":"少发","tipt":"感冒指数","des":"各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,但因气温较高且风力较强,请适当降低运动强度并注意户外防风。"},{"title":"紫外线强度","zs":"中等","tipt":"紫外线强度指数","des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}]
* weather_data : [{"date":"周五 06月02日 (实时:30℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东北风3-4级","temperature":"30 ~ 21℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"晴转多云","wind":"东北风3-4级","temperature":"30 ~ 20℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"东风3-4级","temperature":"26 ~ 19℃"},{"date":"周一","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/zhenyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhenyu.png","weather":"阵雨","wind":"东南风微风","temperature":"25 ~ 20℃"}]
*/
var currentCity: String? = null
var pm25: String? = null
var index: List? = null
var weather_data: List? = null
class IndexBean {
/**
* title : 穿衣
* zs : 热
* tipt : 穿衣指数
* des : 天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。
*/
var title: String? = null
var zs: String? = null
var tipt: String? = null
var des: String? = null
}
class WeatherDataBean {
/**
* date : 周五 06月02日 (实时:30℃)
* dayPictureUrl : http://api.map.baidu.com/images/weather/day/duoyun.png
* nightPictureUrl : http://api.map.baidu.com/images/weather/night/duoyun.png
* weather : 多云
* wind : 东北风3-4级
* temperature : 30 ~ 21℃
*/
var date: String? = null
var dayPictureUrl: String? = null
var nightPictureUrl: String? = null
var weather: String? = null
var wind: String? = null
var temperature: String? = null
}
}
}
最后就是在activity里面使用了
注意 里面的继承和override
匿名内部类的用法
控件的使用
package com.recovery.kotlin
import android.app.Activity
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.recovery.kotlin.model.WeatherData
import com.recovery.kotlin.request.APIService
import kotlinx.android.synthetic.main.activity_main.*
import rx.Subscriber
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
/**
* Created by tom on 2017/5/22.
*/
class MainActivity : Activity(), View.OnClickListener {
override fun onClick(v: View?) {
if (!TextUtils.isEmpty(et_city.text)) {
//用了kotlin 扩展以后直接用id来获得view,再也不用用findviewById了
APIService.getCityWeather(et_city.text.toString())
.subscribeOn(Schedulers.io())
// object : 是匿名内部类的标准用法,高级的用法我还没研究到
.observeOn(AndroidSchedulers.mainThread())?.subscribe( object :Subscriber(){
override fun onNext(weather: WeatherData?) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
var weatherDetail: WeatherData.ResultsBean.WeatherDataBean? = weather?.results?.get(0)?.weather_data?.get(0);
tv_weather_value.setText(weather?.results?.get(0)?.currentCity+ weatherDetail?.date+weatherDetail?.temperature)
}
override fun onCompleted() {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onError(e: Throwable?) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(this@MainActivity, "请求失败", Toast.LENGTH_SHORT).show()
}
}
)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
tv_search.setOnClickListener(this)
}
}