-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http): 解决httplogging body级别时导致retrofit的@stream注解失败,下载进度异常的情况
- Loading branch information
Showing
8 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-keep class okhttp3.** { *;} | ||
-keep class com.hss01248.network.logging.** { *;} |
52 changes: 52 additions & 0 deletions
52
network-hooks-logging/src/main/java/com/hss01248/network/logging/HttpLogggingAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.hss01248.network.logging; | ||
|
||
import android.text.TextUtils; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
|
||
import okhttp3.Headers; | ||
|
||
/** | ||
* @Despciption todo | ||
* @Author hss | ||
* @Date 25/10/2022 14:08 | ||
* @Version 1.0 | ||
*/ | ||
@Aspect | ||
public class HttpLogggingAspect { | ||
|
||
/** | ||
* 防止HttpLoggingInterceptor.Level.BODY时,retrofit的@Stream注解失效 | ||
* @param joinPoint | ||
* @return | ||
* @throws Throwable | ||
*/ | ||
@Around("execution(* okhttp3.logging.HttpLoggingInterceptor.bodyHasUnknownEncoding(..))") | ||
public Object setWebViewClient(ProceedingJoinPoint joinPoint) throws Throwable{ | ||
Headers headers = (Headers) joinPoint.getArgs()[0]; | ||
//原始逻辑 | ||
String contentEncoding = headers.get("Content-Encoding"); | ||
if(contentEncoding != null | ||
&& (contentEncoding.equalsIgnoreCase("identity") || contentEncoding.equalsIgnoreCase("gzip"))){ | ||
return false; | ||
} | ||
//新增逻辑 | ||
String type = headers.get("Content-Type"); | ||
if(!TextUtils.isEmpty(type)){ | ||
if(type.contains("text") || type.contains("json") || type.contains("xml") || type.contains("x-www-form-urlencoded")){ | ||
return false; | ||
} | ||
} | ||
String type2 = headers.get("flipper-body-type"); | ||
if(!TextUtils.isEmpty(type2)){ | ||
if(type2.contains("text") || type2.contains("json") || type2.contains("xml") || type2.contains("x-www-form-urlencoded")){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...g/src/main/java/com/hss01248/network/logging/PrintReqeustBodyTypeToHeaderInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.hss01248.network.logging; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* @Despciption todo | ||
* @Author hss | ||
* @Date 25/10/2022 15:16 | ||
* @Version 1.0 | ||
*/ | ||
@Deprecated | ||
public class PrintReqeustBodyTypeToHeaderInterceptor implements Interceptor { | ||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
RequestBody requestBody = request.body(); | ||
if(requestBody != null){ | ||
if (requestBody.contentType() != null) { | ||
MediaType mediaType = requestBody.contentType(); | ||
String type = mediaType.toString(); | ||
Request build = request.newBuilder().addHeader("flipper-body-type", type).build(); | ||
return chain.proceed(build); | ||
} | ||
} | ||
return chain.proceed(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters