Commit 75c7f009 authored by 王雷's avatar 王雷

新增针对协程网络请求的异常回调

parent 9bc34bf6
/*
* Copyright (C) 2015 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qimai.android.fetch.callAdapter
import android.util.Log
import com.qimai.android.fetch.convert.BizException
import com.qimai.android.fetch.convert.GsonParseErrorException
import com.qimai.android.fetch.convert.ServerResponseException
import com.qimai.android.fetch.interceptors.GsonParseError
import com.qimai.android.fetch.interceptors.ServerError
import okhttp3.Headers
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor
import okio.Buffer
import okio.Timeout
import retrofit2.*
import java.io.IOException
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.*
import java.util.concurrent.Executor
/****
* 针对于使用协程请求的方法,写的代理最终发起网络请求的类 模仿自 DefaultCallAdapterFactory 主要是为了获取请求失败时候的call
*
* @param ServerError 目前订的是服务器返回false 的回调
* @param gsonParseError gson解析异常时候的回调
*/
class KotlinCoroutinesCallAdapterFactory(
var serverError: ServerError? = null,
var gsonParseError: GsonParseError? = null
) :
CallAdapter.Factory() {
override fun get(
returnType: Type, annotations: Array<Annotation>, retrofit: Retrofit
): CallAdapter<*, *>? {
if (getRawType(returnType) != Call::class.java) {
return null
}
require(returnType is ParameterizedType) { "Call return type must be parameterized as Call<Foo> or Call<? extends Foo>" }
val responseType = Utils.getParameterUpperBound(0, returnType)
return object : CallAdapter<Any?, Call<*>> {
override fun responseType(): Type {
return responseType
}
override fun adapt(call: Call<Any?>): Call<Any?> {
return DelegateCall(call, serverError, gsonParseError)
}
}
}
internal class DelegateCall<T>(
val delegate: Call<T>, var serverError: ServerError? = null,
var gsonParseError: GsonParseError? = null
) : Call<T> by delegate {
override fun enqueue(callback: Callback<T>) {
Log.d(TAG, "enqueue11111: ")
delegate.enqueue(object : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
callback.onResponse(call, response)
}
override fun onFailure(call: Call<T>, t: Throwable) {
callback.onFailure(call, t)
val request = call.request()
val requestStartMessage = ("--> "
+ request.method()
+ ' ' + request.url()
)
//业务逻辑错误
if (t is BizException) {
serverError?.apply {
this.serverError("${t.message}- ${t.code} - ${t.traceId} - $requestStartMessage")
}
} else if (t is GsonParseErrorException) {
//gson解析异常
gsonParseError?.apply {
this.parseError("${t.message} - ${t.traceId}- $requestStartMessage")
}
} else if (t is ServerResponseException) {
serverError?.apply {
this.serverError("${t.message} - ${t.traceId}- $requestStartMessage")
}
}
}
})
}
}
companion object {
private const val TAG = "DefaultCallAdapterFacto"
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment