NetworkProxy

Additional

Language
Java
Version
N/A
Created
Dec 8, 2018
Updated
Nov 28, 2019 (Retired)
Owner
Quan Vu (nongdenchet)
Contributor
Quan Vu (nongdenchet)
1
Activity
Badge
Generate
Download
Source code

NetworkProxy

A network proxy library to intercept and mock response from OkHttp

Getting started

In your root build.gradle:

allprojects {
    repositories {
        // Other configurations ...
        jcenter()
    }
}

In your app/build.gradle:

dependencies {
  debugImplementation 'com.github.nongdenchet:networkproxy:0.7.0'
  releaseImplementation 'com.github.nongdenchet:networkproxy-no-op:0.7.0'
}

Since OkHttp now requires Android 5.0+ (API level 21+) and Java 8+, we need to add:

android {
  // other configuration...
  defaultConfig {
    // other configuration...
    minSdkVersion 21
  }
  
  // other configuration...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

Add the Interceptor to your OkHttp builder:

new OkHttpClient.Builder()
  // other configuration...
  .addInterceptor(NetworkProxy.interceptor())
  // other configuration...
  .build();

Using with desktop app

  • In your Application class:
public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    NetworkProxy.init(this, 9000); // 9000 is the port that is later use to connect from Desktop app
    // Normal app init code...
  }
}
  • Connect your device with adb and run adb forward tcp:8000 tcp:9000 to connect port 8000 of your computer and port 9000 of your device
  • We provide a client desktop app to use and interact with pending response
  • Demo:

Using without desktop app

  • In your Application class:
public class ExampleApplication extends Application {

  @Override public void onCreate() {
    super.onCreate();
    NetworkProxy.init(this);
    // Normal app init code...
  }
}
  • To mock data from a pending response
adb shell "am broadcast -a com.rain.networkproxy.INSTRUCTION -n [YOUR_APP_PACKAGE]/com.rain.networkproxy.NPReceiver \
    --es data '{"id": "0", "status": 200}' \
    --es body '"'{"id": 1, "title": "hello"}'"'"

"id": "0" is the pending response id. You can get it through the UI or from Logcat (Put NetworkProxy as filter to see the logs)

  • Demo:

License

Copyright 2018 Quan Vu.

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.