gpt4 book ai didi

android - 如何在 Kotlin android 改造中使用 enque 方法

转载 作者:太空狗 更新时间:2023-10-29 13:04:56 25 4
gpt4 key购买 nike

我正在尝试使用改造和 kotlin 语言从网络服务获得响应。但我无法调用enque 方法。

这是 retrofit 客户端

    class WebService() {

companion object {
fun createService(isAddToken: Boolean): WebServiceApi {
val logging = HttpLoggingInterceptor()
val httpClient = OkHttpClient.Builder()
var retrofit: Retrofit

httpClient.addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
.header(CONTENT_TYPE, "application/json")
.header(API_USER_NAME, AUTH_USERNAME)
.header(API_PASSWORD, AUTH_PASSWORD)
.header(LANGUAGE_CODE, "en")
if (isAddToken) {
requestBuilder.header(TOKEN,
"" /*DataGenerator.getAuthToken(context)*/)
}

requestBuilder.method(original.method(), original.body())
val request = requestBuilder.build()
chain.proceed(request)
}

// set your desired log level
if (BuildConfig.DEBUG) {
logging.level = HttpLoggingInterceptor.Level.BODY
// add logging as last interceptor
httpClient.addInterceptor(logging)
}

// Timeout handling
val client = httpClient.readTimeout(20,
TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.build()

if (BuildConfig.DEBUG) {
retrofit = Retrofit.Builder()
.baseUrl(WebServiceConstants.LIVE_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
} else {
retrofit = Retrofit.Builder()
.baseUrl(WebServiceConstants.LIVE_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()

}

return retrofit.create(WebServiceApi::class.java);
}
}
}

这是我在按钮上调用 api 的 MainActivity 代码

 val parameterOTP = SendParameterOTP()
parameterOTP.phoneNumber = phoneNumber

var serviceAPI = WebService.createService(false)
serviceAPI.enque() \\Unable to understand how i call enques**

我尝试使用 rxjava 的示例,它工作正常。但是我想使用 enque 方法,因为我想实现自定义回调以重试。请帮忙

最佳答案

尝试使用这个:

val call = createService(isAddToken).methodNameInWebServiceApi(params)
call.enqueue(object : Callback<ResponseObject> {
override fun onFailure(call: Call<ResponseObject>?, t: Throwable?) {
Log.e("erroe", "")
}

override fun onResponse(call: Call<ResponseObject>?, response: Response<ResponseObject>?) {

}
})

关于android - 如何在 Kotlin android 改造中使用 enque 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49573168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com