Commit 1ff6b395 by amateur

1

1 parent 96d314d9
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) / 1e6d, bodyStr));
return response.newBuilder().body(ResponseBody.Companion.create(bodyStr, resBody.contentType())).build();
}
}
}
......@@ -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;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!