Skip to content

Commit

Permalink
fix(http): 解决httplogging body级别时导致retrofit的@stream注解失败,下载进度异常的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
hss01248 committed Oct 25, 2022
1 parent 8cf6df5 commit 7971a7e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ dependencies {

api 'io.github.didi.dokit:dokitx-plugin:3.5.0.1'

implementation 'com.github.hss01248.HttpUtil2:http:3.0.5'
implementation 'com.github.hss01248.HttpUtil2:http:3.1.2'


}

//com.github.hss01248.flipperUtil
configurations {
all*.exclude group: 'com.github.hss01248.flipperUtil'
//all*.exclude group: 'com.github.hss01248.flipperUtil'
// all*.exclude group: 'me.weishu', module: 'free_reflection'
}

Expand Down
31 changes: 28 additions & 3 deletions app/src/main/java/com/hss01248/flipperdemo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.hss01248.flipperdemo;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.facebook.flipper.plugins.network.NetworkReporter;
import com.facebook.flipper.plugins.network.RequestBodyParser;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hss01248.flipper.DBAspect;
import com.hss01248.flipper.FlipperUtil;
import com.hss01248.http.ConfigInfo;
import com.hss01248.http.HttpUtil;
import com.hss01248.http.callback.MyNetCallback;
Expand All @@ -35,6 +39,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -46,6 +51,7 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import okio.Buffer;

public class MainActivity extends AppCompatActivity {

Expand All @@ -57,6 +63,8 @@ protected void onCreate(Bundle savedInstanceState) {
//.addInterceptor(new MyAppHelperInterceptor())
.retryOnConnectionFailure(false).build();



XXPermissions.with(this).permission(Permission.MANAGE_EXTERNAL_STORAGE)
.request(new OnPermissionCallback() {
@Override
Expand Down Expand Up @@ -253,25 +261,42 @@ public void onSuccess(Object result) {
* @param view
*/
public void downloadLargeFile(View view) {
String url = "https://media.w3.org/2010/05/sintel/trailer.mp4";
//String url = "https://media.w3.org/2010/05/sintel/trailer.mp4";
String url = "https://services.gradle.org/distributions/gradle-6.5-rc-1-docs.zip";
ProgressDialog dialog = new ProgressDialog(this);
dialog.setCanceledOnTouchOutside(false);

HttpUtil.download(url)
.setFileDownlodConfig(FileDownlodConfig.newBuilder()
.fileDir(getExternalFilesDir("down").getAbsolutePath())
.build())
.callback(new MyNetCallback<ResponseBean<FileDownlodConfig>>() {
@Override
public void onSuccess(ResponseBean<FileDownlodConfig> response) {
dialog.dismiss();
ToastUtils.showShort("success");

}

@Override
protected void onStart() {
super.onStart();
dialog.show();
}

@Override
public void onProgressChange(long transPortedBytes, long totalBytes, ConfigInfo info) {
super.onProgressChange(transPortedBytes, totalBytes, info);
LogUtils.i("transPortedBytes:"+transPortedBytes+"--totalBytes:"+totalBytes);
//LogUtils.i("transPortedBytes:"+transPortedBytes+"--totalBytes:"+totalBytes);
String percent = transPortedBytes*100f/totalBytes+"%\n"+
ConvertUtils.byte2FitMemorySize(transPortedBytes,1)+"/"+ConvertUtils.byte2FitMemorySize(totalBytes,1);
dialog.setMessage(percent);

}

@Override
public void onError(String msgCanShow) {
dialog.dismiss();
ToastUtils.showLong(msgCanShow);

}
Expand Down
12 changes: 1 addition & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,7 @@ buildscript {
task clean(type: Delete) {
delete rootProject.buildDir
}
allprojects {
repositories {
/* maven {
url 'https://nexus.hss01248.tech/repository/jitpack_proxy'
credentials {
username "fetcher"
password "123456qt"
}
}*/
}
}


//url 'https://jitpack.io'
//动态修改/优化repo,会造成idea代码报错. 只能加,不能减?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyAppHelperInterceptor implements Interceptor {
Expand Down Expand Up @@ -74,8 +76,19 @@ public Response intercept(Chain chain) throws IOException {
context = activity.getClass().getSimpleName();
}
id = UUID.randomUUID().toString();

String bodyType = "";
RequestBody requestBody = request.body();
if(requestBody != null){
MediaType mediaType = requestBody.contentType();
if(mediaType!=null){
bodyType = mediaType.toString();
}
}

request = request.newBuilder()
.header(KEY_REQUEST_ID, id)
.header(KEY_FLIPPER_PREFIX+"body-type", bodyType)
.header(KEY_FLIPPER_PREFIX+"top-activity", context)
.build();
requestBodyMap.put(id,bodyMetaData);
Expand Down
2 changes: 2 additions & 0 deletions network-hooks-logging/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-keep class okhttp3.** { *;}
-keep class com.hss01248.network.logging.** { *;}
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;
}


}
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);
}
}
8 changes: 8 additions & 0 deletions remote3.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ static void assignRepos(Project project){
}

mavenLocal()
/* maven {
//加到了group里,直接使用maven-public, 无需显式使用jitpack_proxy2
url "https://nexus.hss01248.tech/repository/jitpack_proxy/"
*//*credentials{
username "cccc"
password "ccccc"
}*//*
}*/
mavenCentral()
google()
jcenter()
Expand Down

0 comments on commit 7971a7e

Please sign in to comment.