ktor 這是乙個 kotlin 官方打造的 web 框架,除了提供 http 服務外,ktor 還為和客戶端提供了配套的庫,用來進行 http 請求。當然我們已經有retrofit
等其他庫完成類似功能,但是作為kotlin親兒子,ktor
與coroutine
的搭配更加天衣無縫。
接下來我們通過乙個小例子學習一下ktor在android上的使用。
retrofit
一樣,ktor
在http通訊、json解析等功能模組提供了多個可選項,我們選擇使用okhttp
和kotlinx.serialization
dependencies
dependencies
dependencies
'kotlinx-serialization'
dependencies
通過openweather 提供的api獲取天氣資訊
基於mvvm開發,viewmodel
封裝http請求,將返回的資料通過livedata
通知到ui
基於okhttp
建立乙個httpclient ,整個**非常dsl
}通過httpclient就可以進行http請求了。
viewmodelscope.launch
開啟協程,httpclient.post
進行非同步請求。
val openweathermaplivedata = mutablelivedata()
fungetopenweathermapjson()
openweathermaplivedata.
postvalue
(openweathermapjson)
}catch
(e:exception)
}}
json解析後得到以下data class
@serializable
data
class
openweathermapjson
(val coord: coord,
val weather: list
,val base: string,
val main: main,
val visibility: int,
val wind: wind,
val clouds: clouds,
val dt: int,
val sys: sys,
val timezone: int,
val id: int,
val name: string,
val cod: int
)
訂閱viewmodel,獲取資料後更新view
class mainactivity :()
viewmodel.openweathermaplivedata.
observe
(this
, observer )}
}
雖然retrofit
現在也支援coroutine
的使用,但是ktor
中使用coroutine
的成本更低,而且ktor支援kotlin mpp,當有跨平台需求時ktor絕對是乙個好選擇。
《使用Jmeter進行批量傳送http請求》
本文主要針對批量介面傳送資料 1 先新增乙個testplan 2 在測試計畫中新增乙個執行緒組 6.1 名稱 自由填寫便於解讀 6.2 協議 http協議 6.4 方法 post get 6.5 路勁 為介面的請求路勁 6.6 內容編碼 utf 8 6.7 錄入訊息體資料,需要引數據批量傳送,訊息體...
Python使用指定埠進行http請求的例子
使用requests庫 s.get https 程式設計客棧 baidu.com 我用wireshark測試發現是走的54321埠。使用pycurl庫 c pycurl.curl c.setopt c.url,c.setopt c.localport,54321 c.setopt c.localpo...
C 後台進行Http請求
1.get請求 新建乙個webform專案,新增乙個按鈕,定義click事件 protected void button1 click object sender,eventargs e private void responsecallback iasyncresult result 函式 2.p...