我想从通过Paypal进行首次通话中获取访问令牌
我将Curl转换为Kotlin代码
这里我正在对API调用进行改造。
The sample data is attached here
AutonginMake USMakename
1 Acura Acura
2 Aston Martin Aston Martin
3 Audi Audi
4 Bentley Bentley
5 BMW BMW
6 Buick Buick
7 Cadillac Cadillac
8 Chevrolet Chevrolet
9 Chrysler Chrysler
10 Dodge Dodge
11 Ford Ford
12 GMC GMC
13 Honda Honda
14 HUMMER Hummer
I took the count of above autonginmake and US make and polotted..My requirement is to list this makes when clicking on corresponding regions of pie chart
#packages needed
library(plotly)
library(shiny)
library(DBI)
library(RMySQL)
#connecting db
dealerinventory1<-dbConnect(RMySQL::MySQL(), user='ghhjjl',
password='dfgfdgdg!',
host='hfghfh',
dbname='hhthhq23u')
uscount1=dbGetQuery(dealerinventory1,
'SELECT count(distinct makename) as USmakes FROM dealer_inventory.CarQuery;')
autongincount1=dbGetQuery(dealerinventory1,
'SELECT count(distinct makename) as autonginmakes FROM dealer_inventory.car_inventory_json_lookup;')
usandautongintable <- c(autongincount1,uscount1)
usandautongintable
label <- c(paste("Autongin Count: ", autongincount1),paste("US Industry Count: ", uscount1))
label
unlist <- as.numeric(unlist(usandautongintable))
typeof(unlist)
#table used for plotting
table<- as.data.frame(usandautongintable)
table
#for plotting pie chart
plotpie<- plot_ly(table, labels = label,values = unlist, type = "pie") %>%
layout(title = 'Comparison of Makes',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
plotpie
library(shiny)
library(plotly)
ui= fluidPage(
plotlyOutput("plot")
)
server1<- function(input,output){
output$plot=renderPlotly({
plot_ly(table, labels = label,values = unlist, type = "pie") %>%
layout(title = 'Comparison of Makes',
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
})
}
shinyApp(ui,server1)
如何使用Kotlin和Retofit实现此目的?
答案 0 :(得分:1)
interface PayPalClient {
@FormUrlEncoded
@POST("/v1/oauth2/token")
fun getAccessToken(
@Header("Authorization") credentials: String,
@Field("grant_type") grantType: String
): Single<PayPalAccessToken>}
MainActivity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val credentials = Credentials.basic(CLIENT_ID, CLIENT_SECRET)
initRetrofit().getAccessToken(credentials, "client_credentials")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ token -> Log.d(TAG, token.accessToken) },
{ error -> Log.d(TAG, error.localizedMessage) }
)
}
private fun initRetrofit(): PayPalClient {
return Retrofit.Builder()
.baseUrl("https://api.sandbox.paypal.com")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build().create(PayPalClient::class.java)
}