摘要:前言使用和以及基于配置网络请求端口,并使用百科的搜索接口获得搜索词条相关数目步骤引用库创建服务接口由于需要的变量有些多,所以使用来统一管理变量,在中定义。总结在分线程中无法改变,也就是说必须在线程中才能改变布局。
前言使用Retrofit 和 Rxjava以及基于kotlin配置网络请求端口,并使用Wiki百科的搜索接口获得搜索词条相关数目:https://en.wikipedia.org/w/api.php");
步骤引用库:
//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"
创建服务接口
interface WikiApiService{
@GET("api.php")
fun hitCountCheck(@QueryMap options:Map<String,String>):Observable
//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,是我们自己定义的获取数据类,因为我们想获取类似这种数据:
{ batchcomplete: "", continue: { sroffset: 10, continue: "-||" }, query: { searchinfo: { totalhits: 16776 //target data }, search: [...
创建数据索引对象
其中totalhits是我们想获取的数据,我们需要results -> query -> searchinfo -> totalhits ,所以我们需要创建一个新的object来存放数据索引:
object Model {
data class Result(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)
}
在Activity中使用懒加载来创建observable实例并创建可取消的disposable:
private var disposable: Disposable");null // disposable is a cancelable object
private val wikiApiServe by lazy {
WikiApiService.create()
}
然后创建功能函数:
@SuppressLint("SetTextI18n")
private fun beginSearch(srsearch : String){
val options = HashMap()
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百科中的数目。
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布局。
创建接口服务的方法可以在interface中创建,也可以在Activity中创建,在interface中需要使用companion object,然后在Activity中实例化。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/6799.html
摘要:前言使用和以及基于配置网络请求端口,并使用百科的搜索接口获得搜索词条相关数目步骤引用库创建服务接口由于需要的变量有些多,所以使用来统一管理变量,在中定义。总结在分线程中无法改变,也就是说必须在线程中才能改变布局。前言 使用Retrofit 和 Rxjava以及基于kotlin配置网络请求端口,并使用Wiki百科的搜索接口获得搜索词条相关数目:https://en.wikipedia.org/...
摘要:自定义仿京东手势解锁掘金早上看个,忘了吃饭,最后定位到的问题,反馈了。动画仿新浪微博雷达搜索效果掘金前言在应用中使用动画,可以给用户带来良好的交互体验。掘金该应用内容灵感来源于应用。 自定义 view——仿京东手势解锁 - Android - 掘金早上看个bug,忘了吃饭,最后定位到ROM的问题,反馈了。忙完了是时候享受了,现在戴上耳机,写个清爽点的文章。虽然很简单,刷刷存在感也好啊,...
摘要:使用编写,基于的极速开发框架我们在开发应用程序的时候其实会有很多通用的代码,比方说很常见的页面的几种基本状态的切换正常加载失败加载中空页面。当然,这也是促使我开源出这个使用编写的,基于架构的极速开发框架的主要原因。 KCommon-使用Kotlin编写,基于MVP的极速开发框架 我们在开发Android应用程序的时候其实会有很多通用的代码,比方说很常见的页面的几种基本状态的切换:正常、...
摘要:让你收获满满码个蛋从年月日推送第篇文章一年过去了已累积推文近篇文章,本文为年度精选,共计篇,按照类别整理便于读者主题阅读。本篇文章是今年的最后一篇技术文章,为了让大家在家也能好好学习,特此花了几个小时整理了这些文章。 showImg(https://segmentfault.com/img/remote/1460000013241596); 让你收获满满! 码个蛋从2017年02月20...
阅读 746·2021-11-25 09:43
阅读 1420·2021-10-25 09:47
阅读 2354·2019-08-30 13:46
阅读 641·2019-08-29 13:45
阅读 1160·2019-08-26 13:29
阅读 2871·2019-08-23 15:30
阅读 969·2019-08-23 14:17
阅读 1216·2019-08-23 13:43