Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
Widget
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Android Widget
Widget
Commits
75c7f009
Commit
75c7f009
authored
Jan 19, 2021
by
王雷
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增针对协程网络请求的异常回调
parent
9bc34bf6
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
646 additions
and
0 deletions
+646
-0
KotlinCoroutinesCallAdapterFactory.kt
...d/fetch/callAdapter/KotlinCoroutinesCallAdapterFactory.kt
+108
-0
Utils.java
.../main/java/com/qimai/android/fetch/callAdapter/Utils.java
+538
-0
No files found.
fetch/src/main/java/com/qimai/android/fetch/callAdapter/KotlinCoroutinesCallAdapterFactory.kt
0 → 100644
View file @
75c7f009
/*
* 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
fetch/src/main/java/com/qimai/android/fetch/callAdapter/Utils.java
0 → 100644
View file @
75c7f009
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment