Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ac8021c

Browse files
committed
netspylib-isNetSpy
1 parent 416c26f commit ac8021c

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

app/src/main/java/com/zhxh/coroutines/model/KotlinNetService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class KotlinNetService : BaseNetService() {
1515
}
1616

1717
override fun getInterceptorList(): List<Interceptor> {
18-
return listOf(NetSpyInterceptor(BaseApplication.instance), StethoInterceptor())
18+
return listOf(NetSpyInterceptor(BaseApplication.instance, true), StethoInterceptor())
1919
}
2020

2121
override val baseUrl: String

app/src/main/java/com/zhxh/coroutines/ui/netspy/MainNetSpyActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public void onClick(View view) {
4040
private OkHttpClient getClient(Context context) {
4141
return new OkHttpClient.Builder()
4242
// Add a NetSpyInterceptor instance to your OkHttp client
43-
.addInterceptor(new NetSpyInterceptor(context))
43+
.addInterceptor(new NetSpyInterceptor(context, true))
4444
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
4545
.build();
4646
}
4747

4848
private void launchNetSpyDirectly() {
4949
// Optionally launch NetSpy directly from your own app UI
50-
startActivity(NetSpy.getLaunchIntent(this, true));
50+
startActivity(NetSpy.getLaunchIntent(this));
5151
}
5252

5353
private void doHttpActivity() {

netspylib/src/main/java/com/creditease/netspy/NetSpy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212
public class NetSpy {
1313

14-
public static Intent getLaunchIntent(Context context, boolean isSpy) {
15-
if (isSpy) {
14+
public static Intent getLaunchIntent(Context context) {
15+
if (NetSpyInterceptor.isNetSpy) {
1616
return new Intent(context, NetSpyHomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1717
} else {
1818
return new Intent();

netspylib/src/main/java/com/creditease/netspy/NetSpyInterceptor.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ public enum Period {
6666
private boolean showNotification;
6767
private long maxContentLength = 250000L;
6868

69+
static boolean isNetSpy;
70+
6971
/**
7072
* @param context The current Context.
7173
*/
72-
public NetSpyInterceptor(Context context) {
74+
public NetSpyInterceptor(Context context, boolean isNetSpy) {
75+
NetSpyInterceptor.isNetSpy = isNetSpy;
7376
this.context = context.getApplicationContext();
7477
notificationHelper = new NotificationHelper(this.context);
7578
showNotification = true;
@@ -113,6 +116,12 @@ public NetSpyInterceptor retainDataFor(Period period) {
113116

114117
@Override
115118
public Response intercept(Chain chain) throws IOException {
119+
120+
if (!isNetSpy) {
121+
Request request = chain.request();
122+
return chain.proceed(request);
123+
}
124+
116125
Request request = chain.request();
117126

118127
RequestBody requestBody = request.body();
@@ -254,8 +263,8 @@ private boolean isPlaintext(Buffer buffer) {
254263
private boolean bodyHasUnsupportedEncoding(Headers headers) {
255264
String contentEncoding = headers.get("Content-Encoding");
256265
return contentEncoding != null &&
257-
!contentEncoding.equalsIgnoreCase("identity") &&
258-
!contentEncoding.equalsIgnoreCase("gzip");
266+
!contentEncoding.equalsIgnoreCase("identity") &&
267+
!contentEncoding.equalsIgnoreCase("gzip");
259268
}
260269

261270
private boolean bodyGzipped(Headers headers) {

0 commit comments

Comments
 (0)