[英]How to use enque method in retrofit in Kotlin android
我正在尝试使用改造和kotlin语言从Web服务获得响应。 但我无法调用enque方法。
这是改造客户端
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);
}
}
}
这是MainActivity的代码,我在按钮上调用api
val parameterOTP = SendParameterOTP()
parameterOTP.phoneNumber = phoneNumber
var serviceAPI = WebService.createService(false)
serviceAPI.enque() \\Unable to understand how i call enques**
我尝试使用rxjava的例子,它工作正常。但我想使用enque方法,因为我想实现自定义回调重试。 请帮忙
我使用kotlin提供改装类和api调用,并根据你身边的rxjava进行更改。
class ApiClient {
companion object {
val BASE_URL = "https://simplifiedcoding.net/demos/"
var retrofit: Retrofit? = null
fun getClient(): Retrofit? {
if (retrofit == null) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().apply {
readTimeout(20, TimeUnit.SECONDS)
writeTimeout(20, TimeUnit.SECONDS)
connectTimeout(20, TimeUnit.SECONDS)
addInterceptor(interceptor)
addInterceptor { chain ->
var request = chain.request()
request = request.newBuilder()
.build()
val response = chain.proceed(request)
response
}
}
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit
}
}
}
之后使用下面的代码进行api调用..
var apiInterface: ApiInterface = ApiClient.getClient()!!.create(ApiInterface::class.java)
var hero: Call<List<Hero>>
hero = apiInterface.getData()
hero.enqueue(object : Callback<List<Hero>> {
override fun onFailure(call: Call<List<Hero>>?, t: Throwable?) {
closeDialog(dialog)
Toast.makeText(mContext, t?.message, Toast.LENGTH_SHORT).show()
Log.d("Error:::",t?.message)
}
override fun onResponse(call: Call<List<Hero>>?, response: Response<List<Hero>>?) {
mHeroDataList.clear()
if (response != null && response.isSuccessful && response.body() != null) {
closeDialog(dialog)
mHeroDataList .addAll(response.body()!!)
setAdapter(mHeroDataList)
}
}
})
试着用这个:
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>?) {
}
})
// Retrofit
implementation ("com.squareup.retrofit2:retrofit:$retrofit_version") {
// exclude Retrofit’s OkHttp peer-dependency module and define your own module import
exclude module: "okhttp"
}
// OkHttp and a logging interceptor
implementation("com.squareup.okhttp3:okhttp:$okhttp_version")
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
buildscript {
...
ext.retrofit_version = '2.6.0'
ext.okhttp_version = '4.0.0'
...
}
object ApiClient {
private const val BASE_URL = "https://test.apiary-mock.com/"
private var retrofit: Retrofit? = null
val apiClient: Retrofit
get() {
if (retrofit == null) {
val logging = HttpLoggingInterceptor()
logging.apply {
level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE
}
val client: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
return retrofit!!
}
}
val carRestService = ApiClient.apiClient.create(CarRestService::class.java)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.