-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetWorkInterceptor.java
More file actions
64 lines (59 loc) · 1.89 KB
/
NetWorkInterceptor.java
File metadata and controls
64 lines (59 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import android.util.Log;
import java.io.IOException;
import java.net.URLDecoder;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
/**
* Okhttp请求拦截
* 打印出请求的链接到logcat
* 链接已经拼接好GET请求可以直接链接
*/
public class NetWorkInterceptor implements Interceptor {
public static String TAG = "NetWorkInterceptor";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (BuildConfig.DEBUG) {
String methodName = request.method();
if (methodName.equalsIgnoreCase("GET")) {
Log.i(TAG, "-url--" + methodName + "--" + request.url());
} else if (methodName.equalsIgnoreCase("POST")) {
RequestBody mRequestBody = request.body();
if (mRequestBody != null) {
String msg = "-url--" + methodName + "--" + request.url();
String content;
if (msg.contains("uploadFile")) {
content = "--上传文件内容--";
} else {
content = getParam(mRequestBody);
}
Log.i(TAG, msg + content);
}
}
}
Response response = chain.proceed(request);
return response;
}
/**
* 读取参数
*
* @param requestBody
* @return
*/
private String getParam(RequestBody requestBody) {
Buffer buffer = new Buffer();
String logparm;
try {
requestBody.writeTo(buffer);
logparm = buffer.readUtf8();
logparm = URLDecoder.decode(logparm, "utf-8");
} catch (IOException e) {
e.printStackTrace();
return "";
}
return logparm;
}
}