diff --git a/app/src/main/java/me/jessyan/mvparms/demo/app/GlobalConfiguration.java b/app/src/main/java/me/jessyan/mvparms/demo/app/GlobalConfiguration.java index 7bd3638ce3b8d5cda7aa55798273368d149c46ea..3ad1b7fb0bc54a82fe9bf1d7b44603eb7b54811a 100644 --- a/app/src/main/java/me/jessyan/mvparms/demo/app/GlobalConfiguration.java +++ b/app/src/main/java/me/jessyan/mvparms/demo/app/GlobalConfiguration.java @@ -1,18 +1,18 @@ -/** - * Copyright 2017 JessYan - * - * 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. - */ +/* + * Copyright 2017 JessYan + *
+ * 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 me.jessyan.mvparms.demo.app; import android.app.Application; @@ -54,7 +54,7 @@ public final class GlobalConfiguration implements ConfigModule { @Override public void applyOptions(Context context, GlobalConfigModule.Builder builder) { - if (!BuildConfig.LOG_DEBUG){ //Release 时,让框架不再打印 Http 请求和响应的信息 + if (!BuildConfig.LOG_DEBUG) { //Release 时,让框架不再打印 Http 请求和响应的信息 builder.printHttpLogLevel(RequestInterceptor.Level.NONE); } diff --git a/arms/src/main/java/com/jess/arms/di/module/GlobalConfigModule.java b/arms/src/main/java/com/jess/arms/di/module/GlobalConfigModule.java index d62bb967f971cd38cb3cc73eba5c55ea9df681db..6cd2099a4bd0286771cc95b9d5e9f4d4adeae2d0 100644 --- a/arms/src/main/java/com/jess/arms/di/module/GlobalConfigModule.java +++ b/arms/src/main/java/com/jess/arms/di/module/GlobalConfigModule.java @@ -23,6 +23,8 @@ import android.text.TextUtils; import com.bumptech.glide.Glide; import com.jess.arms.http.BaseUrl; import com.jess.arms.http.GlobalHttpHandler; +import com.jess.arms.http.log.DefaultFormatPrinter; +import com.jess.arms.http.log.FormatPrinter; import com.jess.arms.http.log.RequestInterceptor; import com.jess.arms.http.imageloader.BaseImageLoaderStrategy; import com.jess.arms.http.imageloader.glide.GlideImageLoaderStrategy; @@ -67,6 +69,7 @@ public class GlobalConfigModule { private ClientModule.RxCacheConfiguration mRxCacheConfiguration; private AppModule.GsonConfiguration mGsonConfiguration; private RequestInterceptor.Level mPrintHttpLogLevel; + private FormatPrinter mFormatPrinter; private Cache.Factory mCacheFactory; private GlobalConfigModule(Builder builder) { @@ -82,6 +85,7 @@ public class GlobalConfigModule { this.mRxCacheConfiguration = builder.rxCacheConfiguration; this.mGsonConfiguration = builder.gsonConfiguration; this.mPrintHttpLogLevel = builder.printHttpLogLevel; + this.mFormatPrinter = builder.formatPrinter; this.mCacheFactory = builder.cacheFactory; } @@ -198,6 +202,12 @@ public class GlobalConfigModule { return mPrintHttpLogLevel; } + @Singleton + @Provides + FormatPrinter provideFormatPrinter(){ + return mFormatPrinter == null ? new DefaultFormatPrinter() : mFormatPrinter; + } + @Singleton @Provides Cache.Factory provideCacheFactory(Application application) { @@ -226,6 +236,7 @@ public class GlobalConfigModule { private ClientModule.RxCacheConfiguration rxCacheConfiguration; private AppModule.GsonConfiguration gsonConfiguration; private RequestInterceptor.Level printHttpLogLevel; + private FormatPrinter formatPrinter; private Cache.Factory cacheFactory; private Builder() { @@ -303,6 +314,13 @@ public class GlobalConfigModule { return this; } + public Builder formatPrinter(FormatPrinter formatPrinter){ + if (formatPrinter == null) + throw new NullPointerException("FormatPrinter can not be null"); + this.formatPrinter = formatPrinter; + return this; + } + public Builder cacheFactory(Cache.Factory cacheFactory) { this.cacheFactory = cacheFactory; return this; diff --git a/arms/src/main/java/com/jess/arms/http/log/DefaultFormatPrinter.java b/arms/src/main/java/com/jess/arms/http/log/DefaultFormatPrinter.java index c4cb9f2796f5e71242564957261cb3898a56c5f7..20cffb77a1c7871dc9abde91cef07cbf192653fc 100644 --- a/arms/src/main/java/com/jess/arms/http/log/DefaultFormatPrinter.java +++ b/arms/src/main/java/com/jess/arms/http/log/DefaultFormatPrinter.java @@ -18,22 +18,28 @@ package com.jess.arms.http.log; import android.text.TextUtils; +import com.jess.arms.di.module.GlobalConfigModule; +import com.jess.arms.utils.CharacterHandler; + import java.util.List; +import okhttp3.MediaType; import okhttp3.Request; import timber.log.Timber; /** * ================================================ - * 对 OkHttp 的请求和响应信息进行更规范和清晰的打印 - *
+ * 对 OkHttp 的请求和响应信息进行更规范和清晰的打印, 此类为框架默认实现, 以默认格式打印信息, 若觉得默认打印格式
+ * 并不能满足自己的需求, 可自行扩展自己理想的打印格式
+ *
+ * @see GlobalConfigModule.Builder#formatPrinter(FormatPrinter)
* Created by JessYan on 25/01/2018 14:51
* Contact me
* Follow me
* ================================================
*/
-public class DefaultFormatPrinter {
+public class DefaultFormatPrinter implements FormatPrinter {
private static final String TAG = "ArmsHttpLog";
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String DOUBLE_SEPARATOR = LINE_SEPARATOR + LINE_SEPARATOR;
@@ -57,9 +63,6 @@ public class DefaultFormatPrinter {
private static final String CENTER_LINE = "├ ";
private static final String DEFAULT_LINE = "│ ";
- private DefaultFormatPrinter() {
- throw new UnsupportedOperationException("you can't instantiate me!");
- }
private static boolean isEmpty(String line) {
return TextUtils.isEmpty(line) || N.equals(line) || T.equals(line) || TextUtils.isEmpty(line.trim());
@@ -71,7 +74,8 @@ public class DefaultFormatPrinter {
* @param request
* @param bodyString
*/
- static void printJsonRequest(Request request, String bodyString) {
+ @Override
+ public void printJsonRequest(Request request, String bodyString) {
final String requestBody = LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString;
final String tag = getTag(true);
@@ -87,7 +91,8 @@ public class DefaultFormatPrinter {
*
* @param request
*/
- static void printFileRequest(Request request) {
+ @Override
+ public void printFileRequest(Request request) {
final String tag = getTag(true);
Timber.tag(tag).i(REQUEST_UP_LINE);
@@ -100,17 +105,21 @@ public class DefaultFormatPrinter {
/**
* 打印网络响应信息, 当网络响应时 {{@link okhttp3.ResponseBody}} 可以解析的情况
*
- * @param chainMs
- * @param isSuccessful
- * @param code
- * @param headers
- * @param bodyString
- * @param segments
- * @param message
- * @param responseUrl
+ * @param chainMs 服务器响应耗时(单位毫秒)
+ * @param isSuccessful 请求是否成功
+ * @param code 响应码
+ * @param headers 请求头
+ * @param contentType 服务器返回数据的数据类型
+ * @param bodyString 服务器返回的数据(已解析)
+ * @param segments 域名后面的资源地址
+ * @param message 响应信息
+ * @param responseUrl 请求地址
*/
- static void printJsonResponse(long chainMs, boolean isSuccessful,
- int code, String headers, String bodyString, List