kotlin + retroft+rxjava查询天气

kotlin使用retrofit+rxjava

前言

本文主要为了跟风,顺便研究下kotlin怎么用

建立一个activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:maxLength="6"
        android:id="@+id/et_city"
        />
        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="查询"
            android:layout_alignParentRight="true"
            android:id="@+id/tv_search"
            android:textSize="18dp"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:background="#e6e6e6"
            android:textColor="#ffffffff"
            />
    </RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv_weather_value"
        />
</LinearLayout>

相关依赖导入

  1. 主工程目录下的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<WeatherData>
        }

        public fun getCityWeather(city: String): Observable<WeatherData> {
            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<ResultsBean>? = 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<IndexBean>? = null
        var weather_data: List<WeatherDataBean>? = 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<WeatherData>(){
                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)
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容