使用Ktor實作Android訂閱-Part5

YungHsin
Apr 2, 2021

--

這一Part就是把Part2最後一段的上傳訂單動作串接起來,Android APP串接Http API有很多方式,想說既然都已經拿Ktor當後端來寫,也試試看用Android寫Ktor來串API。

(1) 在build.gradle加入Ktor的library

//ktor client
def ktor_version = '1.3.0'
implementation "io.ktor:ktor-client-android:$ktor_version"
implementation "io.ktor:ktor-client-gson:$ktor_version"

(2) 呼叫在Part3 MyServer寫的validPurchase API

private suspend fun validPurchase(purchase: Purchase): SubscriptionPurchase {
val client = HttpClient(Android) {
install(JsonFeature) {
serializer = GsonSerializer()
}
}
val subscriptionPurchase =
client.get<SubscriptionPurchase>("http://MyServerIP:8080/validPurchase") {
parameter("packageName", purchase.packageName)
parameter("subscriptionId", purchase.sku)
parameter("token", purchase.purchaseToken)
}
return subscriptionPurchase
}

(3)呼叫後回傳的SubscriptionPurchase內容:

{acknowledgementState=0.0, autoRenewing=true, countryCode=TW, developerPayload=, expiryTimeMillis=1617354211715, kind=androidpublisher#subscriptionPurchase, linkedPurchaseToken=xxxxxxxxxxxxxxxxxxxxxx, orderId=xxx.3371–8105–9198–09959, paymentState=1.0, priceAmountMicros=1000000000, priceCurrencyCode=TWD, purchaseType=0.0, startTimeMillis=1617352817196}

總結

介紹Android訂閱從APP到Server的介紹就到這結束了,當然訂閱的細節不是靠這Part1~Part5就能講完,主要還是要看你的訂閱服務的流程跟APP想要怎麼呈現訂閱後的狀態跟啟動對應的服務囉。

以下是本系列各篇的連結:

Part1 : Android訂閱Overview

Part2: 實作Android訂閱 UI

Part3: 實作Ktor驗証訂閱訂單API

Part4:實作Google Pub/Sub API

Part5: APP串接驗證訂閱訂單API

Buy Me a Tea!

--

--

YungHsin
YungHsin

No responses yet