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
b77be41d
Commit
b77be41d
authored
May 19, 2023
by
快乐石头111
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
移除无用log&getExternalCacheDir导致的anr
parent
fdd59633
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
292 additions
and
12 deletions
+292
-12
KotlinCoroutinesCallAdapterFactory.kt
...d/fetch/callAdapter/KotlinCoroutinesCallAdapterFactory.kt
+4
-7
OkHttpConfig.kt
.../main/java/com/qimai/android/fetch/config/OkHttpConfig.kt
+3
-3
ViewModelExtention.kt
...a/com/qimai/android/fetch/extention/ViewModelExtention.kt
+1
-1
AndroidNetWorkTools.kt
.../java/com/qimai/android/fetch/tool/AndroidNetWorkTools.kt
+1
-1
GsonUtils.java
...src/main/java/com/qimai/android/fetch/tool/GsonUtils.java
+283
-0
No files found.
fetch/src/main/java/com/qimai/android/fetch/callAdapter/KotlinCoroutinesCallAdapterFactory.kt
View file @
b77be41d
...
...
@@ -16,16 +16,15 @@
package
com.qimai.android.fetch.callAdapter
import
android.util.Log
import
com.google.gson.Gson
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
com.qimai.android.fetch.tool.GsonUtils
import
okhttp3.Headers
import
okhttp3.MediaType
import
okhttp3.Request
import
okhttp3.logging.HttpLoggingInterceptor
import
okio.Buffer
import
retrofit2.*
import
java.io.EOFException
...
...
@@ -107,7 +106,6 @@ class KotlinCoroutinesCallAdapterFactory(
override
fun
enqueue
(
callback
:
Callback
<
T
>)
{
Log
.
d
(
TAG
,
"enqueue11111: "
)
delegate
.
enqueue
(
object
:
Callback
<
T
>
{
override
fun
onResponse
(
call
:
Call
<
T
>,
response
:
Response
<
T
>)
{
//获取请求信息
...
...
@@ -117,8 +115,7 @@ class KotlinCoroutinesCallAdapterFactory(
//响应信息
val
body
=
response
.
body
()
val
gson
=
Gson
()
val
responseInfo
=
gson
.
toJson
(
body
)
val
responseInfo
=
GsonUtils
.
toJson
(
body
)
val
requestMessage
=
getRequestInfo
(
request
)
successResponseInfo
?.
invoke
(
requestUrl
,
requestMessage
,
responseInfo
)
...
...
@@ -132,7 +129,7 @@ class KotlinCoroutinesCallAdapterFactory(
override
fun
onFailure
(
call
:
Call
<
T
>,
t
:
Throwable
)
{
callback
.
onFailure
(
call
,
t
)
val
gson
=
Gson
()
val
gson
=
Gson
Utils
.
getGson
()
val
request
=
call
.
request
()
val
requestStartMessage
=
RequestInfoBean
(
...
...
@@ -231,7 +228,7 @@ class KotlinCoroutinesCallAdapterFactory(
charset
=
contentType
.
charset
(
Charset
.
forName
(
"UTF-8"
))
}
if
(
isPlaintext
(
buffer
))
{
Log
.
d
(
TAG
,
"getRequestInfo: requestMessage = $requestMessage requestMessage size = ${requestMessage.length} bytes buffer size = ${buffer.size()}"
)
//
Log.d(TAG, "getRequestInfo: requestMessage = $requestMessage requestMessage size = ${requestMessage.length} bytes buffer size = ${buffer.size()}")
requestMessage
.
append
(
buffer
.
readString
(
charset
))
buffer
.
clear
()
requestMessage
.
append
(
...
...
fetch/src/main/java/com/qimai/android/fetch/config/OkHttpConfig.kt
View file @
b77be41d
...
...
@@ -188,12 +188,12 @@ class OkHttpConfig private constructor(builder: Builder) {
* 配置缓存
*/
private
fun
setCacheConfig
()
{
val
externalCacheDir
=
HolderContext
.
getContext
().
externalCacheDir
?:
return
defaultCachePath
=
externalCacheDir
.
path
+
"/RxHttpCacheData"
if
(
isCache
)
{
if
(
isCache
)
{
val
cache
:
Cache
=
if
(!
TextUtils
.
isEmpty
(
cachePath
)
&&
cacheMaxSize
>
0
)
{
Cache
(
File
(
cachePath
!!
),
cacheMaxSize
)
}
else
{
val
externalCacheDir
=
HolderContext
.
getContext
().
externalCacheDir
?:
return
defaultCachePath
=
externalCacheDir
.
path
+
"/RxHttpCacheData"
Cache
(
File
(
defaultCachePath
!!
),
defaultCacheSize
)
}
...
...
fetch/src/main/java/com/qimai/android/fetch/extention/ViewModelExtention.kt
View file @
b77be41d
...
...
@@ -93,7 +93,7 @@ fun <T> ViewModel.flowSafeCall(request: () -> T) = liveData<Resource<T>> {
}
fun
<
T
>
handleResponseError
(
throwable
:
Throwable
?):
Resource
<
T
>
{
Log
.
d
(
TAG
,
"handleResponseError: throwable= $throwable"
)
//
Log.d(TAG, "handleResponseError: throwable= $throwable")
return
when
(
throwable
)
{
null
->
Resource
.
error
(
"程序出错"
,
ErrorData
(
originThrowable
=
throwable
))
is
HttpException
->
when
(
throwable
.
code
())
{
...
...
fetch/src/main/java/com/qimai/android/fetch/tool/AndroidNetWorkTools.kt
View file @
b77be41d
...
...
@@ -15,7 +15,7 @@ import android.net.ConnectivityManager
**/
fun
isNetworkConnected
(
context
:
Context
):
Boolean
{
val
mConnectivityManager
=
context
!!
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
)
as
ConnectivityManager
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
)
as
ConnectivityManager
val
mNetworkInfo
=
mConnectivityManager
.
activeNetworkInfo
if
(
mNetworkInfo
!=
null
)
{
return
mNetworkInfo
.
isAvailable
...
...
fetch/src/main/java/com/qimai/android/fetch/tool/GsonUtils.java
0 → 100644
View file @
b77be41d
package
com
.
qimai
.
android
.
fetch
.
tool
;
import
android.text.TextUtils
;
import
com.google.gson.Gson
;
import
com.google.gson.GsonBuilder
;
import
com.google.gson.reflect.TypeToken
;
import
java.io.Reader
;
import
java.lang.reflect.Type
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.ConcurrentHashMap
;
import
androidx.annotation.NonNull
;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/04/05
* desc : utils about gson
* </pre>
*/
public
final
class
GsonUtils
{
private
static
final
String
KEY_DEFAULT
=
"defaultGson"
;
private
static
final
String
KEY_DELEGATE
=
"delegateGson"
;
private
static
final
String
KEY_LOG_UTILS
=
"logUtilsGson"
;
private
static
final
Map
<
String
,
Gson
>
GSONS
=
new
ConcurrentHashMap
<>();
private
GsonUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
}
/**
* Set the delegate of {@link Gson}.
*
* @param delegate The delegate of {@link Gson}.
*/
public
static
void
setGsonDelegate
(
Gson
delegate
)
{
if
(
delegate
==
null
)
return
;
GSONS
.
put
(
KEY_DELEGATE
,
delegate
);
}
/**
* Set the {@link Gson} with key.
*
* @param key The key.
* @param gson The {@link Gson}.
*/
public
static
void
setGson
(
final
String
key
,
final
Gson
gson
)
{
if
(
TextUtils
.
isEmpty
(
key
)
||
gson
==
null
)
return
;
GSONS
.
put
(
key
,
gson
);
}
/**
* Return the {@link Gson} with key.
*
* @param key The key.
* @return the {@link Gson} with key
*/
public
static
Gson
getGson
(
final
String
key
)
{
return
GSONS
.
get
(
key
);
}
public
static
Gson
getGson
()
{
Gson
gsonDelegate
=
GSONS
.
get
(
KEY_DELEGATE
);
if
(
gsonDelegate
!=
null
)
{
return
gsonDelegate
;
}
Gson
gsonDefault
=
GSONS
.
get
(
KEY_DEFAULT
);
if
(
gsonDefault
==
null
)
{
gsonDefault
=
createGson
();
GSONS
.
put
(
KEY_DEFAULT
,
gsonDefault
);
}
return
gsonDefault
;
}
/**
* Serializes an object into json.
*
* @param object The object to serialize.
* @return object serialized into json.
*/
public
static
String
toJson
(
final
Object
object
)
{
return
toJson
(
getGson
(),
object
);
}
/**
* Serializes an object into json.
*
* @param src The object to serialize.
* @param typeOfSrc The specific genericized type of src.
* @return object serialized into json.
*/
public
static
String
toJson
(
final
Object
src
,
@NonNull
final
Type
typeOfSrc
)
{
return
toJson
(
getGson
(),
src
,
typeOfSrc
);
}
/**
* Serializes an object into json.
*
* @param gson The gson.
* @param object The object to serialize.
* @return object serialized into json.
*/
public
static
String
toJson
(
@NonNull
final
Gson
gson
,
final
Object
object
)
{
return
gson
.
toJson
(
object
);
}
/**
* Serializes an object into json.
*
* @param gson The gson.
* @param src The object to serialize.
* @param typeOfSrc The specific genericized type of src.
* @return object serialized into json.
*/
public
static
String
toJson
(
@NonNull
final
Gson
gson
,
final
Object
src
,
@NonNull
final
Type
typeOfSrc
)
{
return
gson
.
toJson
(
src
,
typeOfSrc
);
}
/**
* Converts {@link String} to given type.
*
* @param json The json to convert.
* @param type Type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
final
String
json
,
@NonNull
final
Class
<
T
>
type
)
{
return
fromJson
(
getGson
(),
json
,
type
);
}
/**
* Converts {@link String} to given type.
*
* @param json the json to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
final
String
json
,
@NonNull
final
Type
type
)
{
return
fromJson
(
getGson
(),
json
,
type
);
}
/**
* Converts {@link Reader} to given type.
*
* @param reader the reader to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
@NonNull
final
Reader
reader
,
@NonNull
final
Class
<
T
>
type
)
{
return
fromJson
(
getGson
(),
reader
,
type
);
}
/**
* Converts {@link Reader} to given type.
*
* @param reader the reader to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
@NonNull
final
Reader
reader
,
@NonNull
final
Type
type
)
{
return
fromJson
(
getGson
(),
reader
,
type
);
}
/**
* Converts {@link String} to given type.
*
* @param gson The gson.
* @param json The json to convert.
* @param type Type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
@NonNull
final
Gson
gson
,
final
String
json
,
@NonNull
final
Class
<
T
>
type
)
{
return
gson
.
fromJson
(
json
,
type
);
}
/**
* Converts {@link String} to given type.
*
* @param gson The gson.
* @param json the json to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
@NonNull
final
Gson
gson
,
final
String
json
,
@NonNull
final
Type
type
)
{
return
gson
.
fromJson
(
json
,
type
);
}
/**
* Converts {@link Reader} to given type.
*
* @param gson The gson.
* @param reader the reader to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
@NonNull
final
Gson
gson
,
final
Reader
reader
,
@NonNull
final
Class
<
T
>
type
)
{
return
gson
.
fromJson
(
reader
,
type
);
}
/**
* Converts {@link Reader} to given type.
*
* @param gson The gson.
* @param reader the reader to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public
static
<
T
>
T
fromJson
(
@NonNull
final
Gson
gson
,
final
Reader
reader
,
@NonNull
final
Type
type
)
{
return
gson
.
fromJson
(
reader
,
type
);
}
/**
* Return the type of {@link List} with the {@code type}.
*
* @param type The type.
* @return the type of {@link List} with the {@code type}
*/
public
static
Type
getListType
(
@NonNull
final
Type
type
)
{
return
TypeToken
.
getParameterized
(
List
.
class
,
type
).
getType
();
}
/**
* Return the type of {@link Set} with the {@code type}.
*
* @param type The type.
* @return the type of {@link Set} with the {@code type}
*/
public
static
Type
getSetType
(
@NonNull
final
Type
type
)
{
return
TypeToken
.
getParameterized
(
Set
.
class
,
type
).
getType
();
}
/**
* Return the type of map with the {@code keyType} and {@code valueType}.
*
* @param keyType The type of key.
* @param valueType The type of value.
* @return the type of map with the {@code keyType} and {@code valueType}
*/
public
static
Type
getMapType
(
@NonNull
final
Type
keyType
,
@NonNull
final
Type
valueType
)
{
return
TypeToken
.
getParameterized
(
Map
.
class
,
keyType
,
valueType
).
getType
();
}
/**
* Return the type of array with the {@code type}.
*
* @param type The type.
* @return the type of map with the {@code type}
*/
public
static
Type
getArrayType
(
@NonNull
final
Type
type
)
{
return
TypeToken
.
getArray
(
type
).
getType
();
}
/**
* Return the type of {@code rawType} with the {@code typeArguments}.
*
* @param rawType The raw type.
* @param typeArguments The type of arguments.
* @return the type of map with the {@code type}
*/
public
static
Type
getType
(
@NonNull
final
Type
rawType
,
@NonNull
final
Type
...
typeArguments
)
{
return
TypeToken
.
getParameterized
(
rawType
,
typeArguments
).
getType
();
}
static
Gson
getGson4LogUtils
()
{
Gson
gson4LogUtils
=
GSONS
.
get
(
KEY_LOG_UTILS
);
if
(
gson4LogUtils
==
null
)
{
gson4LogUtils
=
new
GsonBuilder
().
setPrettyPrinting
().
serializeNulls
().
create
();
GSONS
.
put
(
KEY_LOG_UTILS
,
gson4LogUtils
);
}
return
gson4LogUtils
;
}
private
static
Gson
createGson
()
{
return
new
GsonBuilder
().
serializeNulls
().
disableHtmlEscaping
().
create
();
}
}
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