Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
polysoft-nafmii
/
polysoft-android
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 1ff6b395
authored
Jul 15, 2021
by
amateur
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
1
1 parent
96d314d9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
2 deletions
app/src/main/java/com/polysoft/nafmii/net/OkhttpApi.java
app/src/main/java/com/polysoft/nafmii/net/RequestBack.java
app/src/main/java/com/polysoft/nafmii/net/OkhttpApi.java
View file @
1ff6b39
package
com
.
polysoft
.
nafmii
.
net
;
import
android.util.Log
;
import
androidx.annotation.NonNull
;
import
com.polysoft.nafmii.utils.IOUtils
;
import
org.jetbrains.annotations.NotNull
;
...
...
@@ -13,12 +17,16 @@ import java.util.Map;
import
okhttp3.Call
;
import
okhttp3.Callback
;
import
okhttp3.HttpUrl
;
import
okhttp3.Interceptor
;
import
okhttp3.MediaType
;
import
okhttp3.OkHttpClient
;
import
okhttp3.Request
;
import
okhttp3.RequestBody
;
import
okhttp3.Response
;
import
okhttp3.ResponseBody
;
import
okio.Buffer
;
import
okio.Okio
;
import
okio.Sink
;
public
class
OkhttpApi
{
...
...
@@ -31,7 +39,7 @@ public class OkhttpApi {
if
(
client
==
null
)
{
synchronized
(
OkhttpApi
.
class
)
{
if
(
client
==
null
)
{
client
=
new
OkHttpClient
();
client
=
new
OkHttpClient
.
Builder
().
addInterceptor
(
new
RequestInterceptor
()).
build
();
}
}
}
...
...
@@ -143,4 +151,27 @@ public class OkhttpApi {
}
}
}
private
static
class
RequestInterceptor
implements
Interceptor
{
private
final
String
TAG
=
"okhttp3"
;
@NonNull
@Override
public
Response
intercept
(
@NonNull
Chain
chain
)
throws
IOException
{
long
startTime
=
System
.
nanoTime
();
Request
request
=
chain
.
request
();
Buffer
buffer
=
new
Buffer
();
request
.
body
().
writeTo
(
buffer
);
Log
.
d
(
TAG
,
String
.
format
(
"request:{%n url:%s,%n headers:{%n%s},%n body:%s%n}"
,
request
.
url
(),
request
.
headers
(),
buffer
.
readUtf8
()));
Response
response
=
chain
.
proceed
(
request
);
ResponseBody
resBody
=
response
.
body
();
String
bodyStr
=
resBody
.
string
();
long
endTime
=
System
.
nanoTime
();
Log
.
d
(
TAG
,
String
.
format
(
"response:{%n url:%s,%n headers:{%n%s},%n time:%.1fms,%n body:%s%n "
,
response
.
request
().
url
(),
response
.
headers
(),
(
endTime
-
startTime
)
/
1
e6d
,
bodyStr
));
return
response
.
newBuilder
().
body
(
ResponseBody
.
Companion
.
create
(
bodyStr
,
resBody
.
contentType
())).
build
();
}
}
}
app/src/main/java/com/polysoft/nafmii/net/RequestBack.java
View file @
1ff6b39
...
...
@@ -48,7 +48,6 @@ public abstract class RequestBack<T> {
String
status
=
res
.
getString
(
"status"
);
if
(
"0"
.
equals
(
status
))
{
String
json
=
res
.
getString
(
"data"
);
Log
.
d
(
"onParseResponse"
,
"onParseResponse: "
+
json
);
Type
type
=
this
.
getClass
().
getGenericSuperclass
();
if
(
type
instanceof
ParameterizedType
)
{
ParameterizedType
parameterizedType
=
(
ParameterizedType
)
type
;
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment