Android開發 -- Retrofit + Rxjava + Kotlin => Request API

前言

使用Retrofit 和 Rxjava以及基於kotlin配置網絡請求端口,並使用Wiki百科的搜索接口得到搜索詞條相關數目:https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=li%20zhaojiaphp

步驟

  1. 引用庫:
//Retrofit
    implementation "com.squareup.retrofit2:retrofit:2.4.0"
    //Gson converter
    implementation "com.squareup.retrofit2:converter-gson:2.3.0"
    //String
    implementation "com.squareup.retrofit2:converter-scalars:2.3.0"
    //okhttp
    implementation "com.squareup.okhttp3:okhttp:3.10.0"
    implementation "com.squareup.okhttp3:logging-interceptor:3.8.0"
    //Rxjava
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
    //Rxjava2
    implementation "io.reactivex.rxjava2:rxandroid:2.0.2"
    implementation "io.reactivex.rxjava2:rxjava:2.1.9"
複製代碼
  1. 建立服務接口
interface WikiApiService{
    @GET("api.php")
    fun hitCountCheck(@QueryMap options:Map<String,String>):Observable<Model.Result>
    //an Observable, which is a Rxjava object that could analog as the endpoint fetcher result generator.
    companion object{
        fun create() :WikiApiService{
            val retrofit = Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://en.wikipedia.org/w/")
                .build()
            return retrofit.create(WikiApiService::class.java)
        }
    }
}
複製代碼

因爲須要query的變量有些多,因此使用QueryMap來統一管理變量,在activity中定義HashMap。其中Observable返回的是一個object,是咱們本身定義的獲取數據類,由於咱們想獲取相似這種數據:java

{
 batchcomplete: "",
 continue: {
  sroffset: 10,
  continue: "-||"
 },
 query: {
  searchinfo: {
   totalhits: 16776   //target data
 },
 search: [...
複製代碼
  1. 建立數據索引對象

其中totalhits是咱們想獲取的數據,咱們須要results -> query -> searchinfo -> totalhits ,因此咱們須要建立一個新的object來存放數據索引:react

object Model {
    data class Result(val query: Query)
    data class Query(val searchinfo: SearchInfo)
    data class SearchInfo(val totalhits: Int)
}
複製代碼
  1. 在Activity中使用懶加載來建立observable實例並建立可取消的disposable:
private var disposable: Disposable? = null   // disposable is a cancelable object
private val wikiApiServe by lazy {
    WikiApiService.create()
}
複製代碼

而後建立功能函數:android

@SuppressLint("SetTextI18n")
private fun beginSearch(srsearch : String){
    val options = HashMap<String,String>()
    options["action"] = "query"
    options["format"] = "json"
    options["list"] = "search"
    options["srsearch"] = srsearch
    //Scheduler: Thread control
    disposable = wikiApiServe.hitCountCheck(options)
        .subscribeOn(Schedulers.io())  //subscribeOn(): choose a Thread to produce I/O scheduler:(read&write data、read&write db、change info on the Internet)
        .observeOn(AndroidSchedulers.mainThread())  //observeOn(): choose a Thread to spend AndroidSchedulers.mainThread(),on Android UI(main Thread)
        .subscribe(
            { result -> show_result.text = "${result.query.searchinfo.totalhits} results found"},
            { error -> Toast.makeText(this, error.message, Toast.LENGTH_LONG).show()}
        )
}
複製代碼

在須要獲取的edit text中使用這個功能函數就能夠得到該搜索詞條在wiki百科中的數目。json

get_result.setOnClickListener {
    val text = edit_text.text.toString()
    if (edit_text.text.toString().isNotEmpty()){
        Toast.makeText(this, edit_text.text.toString(), Toast.LENGTH_LONG).show()
        beginSearch(text)
    }
}
複製代碼

總結

在分線程中沒法改變UI,也就是說必須在UI線程(AndroidSchedulers.MainThrea())中才能改變UI佈局。api

建立接口服務的方法能夠在interface中建立,也能夠在Activity中建立,在interface中須要使用companion object,而後在Activity中實例化。網絡

相關文章
相關標籤/搜索