├── sample ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout │ │ │ └── activity_main.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── me │ │ └── wcy │ │ └── mockhttp │ │ └── sample │ │ └── MainActivity.kt ├── proguard-rules.pro └── build.gradle ├── mock-http ├── .gitignore ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ ├── assets │ │ └── mock-http │ │ │ ├── addon │ │ │ └── lint │ │ │ │ ├── json-lint.js │ │ │ │ ├── lint.css │ │ │ │ └── lint.js │ │ │ ├── lib │ │ │ ├── codemirror.css │ │ │ └── jsonlint.js │ │ │ ├── index.html │ │ │ ├── request.html │ │ │ └── mode │ │ │ └── javascript │ │ │ └── javascript.js │ │ └── java │ │ └── me │ │ └── wcy │ │ └── mockhttp │ │ ├── MockHttpEntity.kt │ │ ├── MockHttpOptions.kt │ │ ├── MockHttpUtils.kt │ │ ├── Logger.kt │ │ ├── MockHttpServer.kt │ │ ├── MockHttpInterceptor.kt │ │ └── MockHttp.kt ├── proguard-rules.pro └── build.gradle ├── mock-http-release ├── consumer-rules.pro ├── .gitignore ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── me │ │ └── wcy │ │ └── mockhttp │ │ ├── MockHttpInterceptor.kt │ │ ├── MockHttp.kt │ │ └── MockHttpOptions.kt ├── proguard-rules.pro └── build.gradle ├── art ├── demo.jpg └── architecture.jpg ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mock-http/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /mock-http-release/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /mock-http-release/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /art/demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/art/demo.jpg -------------------------------------------------------------------------------- /art/architecture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/art/architecture.jpg -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':mock-http' 2 | include ':mock-http-release' 3 | include ':sample' 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /sample/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | mock-http 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /mock-http-release/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wangchenyan/mock-http/HEAD/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /sample/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 14 15:29:02 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip 7 | -------------------------------------------------------------------------------- /mock-http/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /mock-http-release/src/main/java/me/wcy/mockhttp/MockHttpInterceptor.kt: -------------------------------------------------------------------------------- 1 | package me.wcy.mockhttp 2 | 3 | import okhttp3.Interceptor 4 | import okhttp3.Response 5 | 6 | /** 7 | * Created by wcy on 2019/5/23. 8 | */ 9 | class MockHttpInterceptor : Interceptor { 10 | 11 | override fun intercept(chain: Interceptor.Chain): Response { 12 | return chain.proceed(chain.request()) 13 | } 14 | } -------------------------------------------------------------------------------- /sample/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | android.enableJetifier=true 10 | android.useAndroidX=true 11 | org.gradle.jvmargs=-Xmx1536m 12 | # When configured, Gradle will run in incubating parallel mode. 13 | # This option should only be used with decoupled projects. More details, visit 14 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 15 | # org.gradle.parallel=true 16 | VERSION_NAME=1.7 -------------------------------------------------------------------------------- /mock-http/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /mock-http-release/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 28 7 | buildToolsVersion "28.0.3" 8 | 9 | defaultConfig { 10 | applicationId "me.wcy.mockhttp.sample" 11 | minSdkVersion 15 12 | targetSdkVersion 28 13 | versionCode 1 14 | versionName VERSION_NAME 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | } 29 | 30 | dependencies { 31 | implementation fileTree(include: ['*.jar'], dir: 'libs') 32 | implementation project(':mock-http') 33 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 34 | implementation 'androidx.appcompat:appcompat:1.2.0' 35 | implementation 'com.squareup.okhttp3:okhttp:3.14.2' 36 | } 37 | -------------------------------------------------------------------------------- /mock-http/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | 6 | dependencies { 7 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 8 | } 9 | } 10 | 11 | apply plugin: 'com.android.library' 12 | apply plugin: 'kotlin-android' 13 | apply plugin: 'com.github.dcendents.android-maven' 14 | 15 | group = 'com.github.wangchenyan' 16 | 17 | android { 18 | compileSdkVersion 28 19 | buildToolsVersion "28.0.3" 20 | 21 | defaultConfig { 22 | minSdkVersion 9 23 | targetSdkVersion 28 24 | versionCode 1 25 | versionName VERSION_NAME 26 | 27 | consumerProguardFiles 'proguard-rules.pro' 28 | } 29 | 30 | compileOptions { 31 | sourceCompatibility JavaVersion.VERSION_1_8 32 | targetCompatibility JavaVersion.VERSION_1_8 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(include: ['*.jar'], dir: 'libs') 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 39 | implementation 'com.koushikdutta.async:androidasync:2.2.1' 40 | compileOnly 'com.squareup.okhttp3:okhttp:3.14.2' 41 | } 42 | -------------------------------------------------------------------------------- /mock-http-release/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | 6 | dependencies { 7 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' 8 | } 9 | } 10 | 11 | apply plugin: 'com.android.library' 12 | apply plugin: 'kotlin-android' 13 | apply plugin: 'com.github.dcendents.android-maven' 14 | 15 | group = 'com.github.wangchenyan' 16 | 17 | android { 18 | compileSdkVersion 28 19 | buildToolsVersion "28.0.3" 20 | 21 | defaultConfig { 22 | minSdkVersion 9 23 | targetSdkVersion 28 24 | versionCode 1 25 | versionName VERSION_NAME 26 | 27 | consumerProguardFiles 'consumer-rules.pro' 28 | } 29 | 30 | compileOptions { 31 | sourceCompatibility JavaVersion.VERSION_1_8 32 | targetCompatibility JavaVersion.VERSION_1_8 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 39 | implementation 'androidx.appcompat:appcompat:1.2.0' 40 | compileOnly 'com.squareup.okhttp3:okhttp:3.14.2' 41 | } 42 | -------------------------------------------------------------------------------- /mock-http-release/src/main/java/me/wcy/mockhttp/MockHttp.kt: -------------------------------------------------------------------------------- 1 | package me.wcy.mockhttp 2 | 3 | import android.annotation.SuppressLint 4 | import android.content.Context 5 | 6 | /** 7 | * MOCK HTTP 8 | * Created by wcy on 2019/5/23. 9 | */ 10 | class MockHttp private constructor() { 11 | 12 | companion object { 13 | fun get(): MockHttp { 14 | return SingletonHolder.instance 15 | } 16 | } 17 | 18 | private object SingletonHolder { 19 | @SuppressLint("StaticFieldLeak") 20 | val instance = MockHttp() 21 | } 22 | 23 | /** 24 | * 获取 MOCK HTTP 配置项 25 | */ 26 | fun getMockHttpOptions(): MockHttpOptions? { 27 | return null 28 | } 29 | 30 | /** 31 | * 设置 MOCK HTTP 配置项
32 | * 需要在 [start] 之前调用,否则将使用默认配置 33 | * 34 | * @param options MOCK HTTP 配置项 35 | * @see MockHttpOptions 36 | */ 37 | fun setMockHttpOptions(options: MockHttpOptions) { 38 | } 39 | 40 | /** 41 | * 启动 MOCK 服务,开始 MOCK
42 | * 如果是多进程应用,只需要在主进程中初始化 43 | * 44 | * @param ctx 上下文 45 | */ 46 | fun start(ctx: Context) { 47 | } 48 | 49 | /** 50 | * 停止 MOCK 服务,释放资源 51 | */ 52 | fun stop() { 53 | } 54 | 55 | /** 56 | * 是否已经启动 57 | */ 58 | fun hasStart(): Boolean { 59 | return false 60 | } 61 | 62 | /** 63 | * 获取 MOCK 服务器地址 64 | */ 65 | fun getMockAddress(): String { 66 | return "" 67 | } 68 | } -------------------------------------------------------------------------------- /mock-http/src/main/assets/mock-http/addon/lint/json-lint.js: -------------------------------------------------------------------------------- 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 | // Distributed under an MIT license: https://codemirror.net/LICENSE 3 | 4 | // Depends on jsonlint.js from https://github.com/zaach/jsonlint 5 | 6 | // declare global: jsonlint 7 | 8 | (function(mod) { 9 | if (typeof exports == "object" && typeof module == "object") // CommonJS 10 | mod(require("../../lib/codemirror")); 11 | else if (typeof define == "function" && define.amd) // AMD 12 | define(["../../lib/codemirror"], mod); 13 | else // Plain browser env 14 | mod(CodeMirror); 15 | })(function(CodeMirror) { 16 | "use strict"; 17 | 18 | CodeMirror.registerHelper("lint", "json", function(text) { 19 | var found = []; 20 | if (!window.jsonlint) { 21 | if (window.console) { 22 | window.console.error("Error: window.jsonlint not defined, CodeMirror JSON linting cannot run."); 23 | } 24 | return found; 25 | } 26 | // for jsonlint's web dist jsonlint is exported as an object with a single property parser, of which parseError 27 | // is a subproperty 28 | var jsonlint = window.jsonlint.parser || window.jsonlint 29 | jsonlint.parseError = function(str, hash) { 30 | var loc = hash.loc; 31 | found.push({from: CodeMirror.Pos(loc.first_line - 1, loc.first_column), 32 | to: CodeMirror.Pos(loc.last_line - 1, loc.last_column), 33 | message: str}); 34 | }; 35 | try { jsonlint.parse(text); } 36 | catch(e) {} 37 | return found; 38 | }); 39 | 40 | }); 41 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /mock-http-release/src/main/java/me/wcy/mockhttp/MockHttpOptions.kt: -------------------------------------------------------------------------------- 1 | package me.wcy.mockhttp 2 | 3 | import android.util.Log 4 | 5 | /** 6 | * MockHttp 配置项 7 | * Created by wcy on 2019/5/26. 8 | */ 9 | class MockHttpOptions private constructor() { 10 | 11 | /** 12 | * MOCK 服务端口,如果已经启动服务,则端口设置不会生效 13 | */ 14 | fun getMockServerPort(): Int { 15 | return 0 16 | } 17 | 18 | /** 19 | * 获取 MOCK HTTP 等待时间 20 | */ 21 | fun getMockSleepTime(): Long { 22 | return 0 23 | } 24 | 25 | /** 26 | * 是否打印日志 27 | */ 28 | fun isLogEnable(): Boolean { 29 | return false 30 | } 31 | 32 | /** 33 | * 日志标签 34 | */ 35 | fun getLogTag(): String { 36 | return "" 37 | } 38 | 39 | /** 40 | * 日志级别 41 | */ 42 | fun getLogLevel(): Int { 43 | return 0 44 | } 45 | 46 | class Builder { 47 | private val options = MockHttpOptions() 48 | 49 | /** 50 | * 设置 MOCK 服务端口 51 | * 52 | * @param port 端口 53 | */ 54 | fun setMockServerPort(port: Int): Builder { 55 | return this 56 | } 57 | 58 | /** 59 | * 设置 MOCK HTTP 等待时间 60 | * 61 | * @param milli 毫秒,默认为 0 62 | */ 63 | fun setMockSleepTime(milli: Long): Builder { 64 | return this 65 | } 66 | 67 | /** 68 | * 设置是否打印日志,开启 Mock 后才能打印日志 69 | * 70 | * @param enable 是否打印日志 71 | */ 72 | fun setLogEnable(enable: Boolean): Builder { 73 | return this 74 | } 75 | 76 | /** 77 | * 设置日志标签 78 | * 79 | * @param tag 日志标签 80 | */ 81 | fun setLogTag(tag: String): Builder { 82 | return this 83 | } 84 | 85 | /** 86 | * 设置日志级别,参考 [Log] 87 | * 88 | * @param level 日志级别,支持 [Log.VERBOSE] [Log.DEBUG] [Log.INFO] [Log.WARN] [Log.ERROR] 89 | */ 90 | fun setLogLevel(level: Int): Builder { 91 | return this 92 | } 93 | 94 | fun build(): MockHttpOptions { 95 | return options 96 | } 97 | } 98 | } -------------------------------------------------------------------------------- /mock-http/src/main/java/me/wcy/mockhttp/MockHttpEntity.kt: -------------------------------------------------------------------------------- 1 | package me.wcy.mockhttp 2 | 3 | import org.json.JSONObject 4 | 5 | /** 6 | * Created by wcy on 2019/5/23. 7 | */ 8 | internal data class MockHttpEntity( 9 | var path: String = "", 10 | var requestUrl: String = "", 11 | var requestMethod: String = "", 12 | var statusCode: Int = 0, 13 | var requestHeader: String = "", 14 | var queryParameter: String = "", 15 | var requestBody: String = "", 16 | var responseHeader: String = "", 17 | var responseBody: String = "") { 18 | 19 | companion object { 20 | fun fromJson(json: String?): MockHttpEntity? { 21 | if (json == null) { 22 | return null 23 | } 24 | 25 | try { 26 | val jsonObject = JSONObject(json) 27 | val entity = MockHttpEntity() 28 | entity.path = jsonObject.optString("path") 29 | entity.requestUrl = jsonObject.optString("requestUrl") 30 | entity.requestMethod = jsonObject.optString("requestMethod") 31 | entity.statusCode = jsonObject.optInt("statusCode") 32 | entity.requestHeader = jsonObject.optString("requestHeader") 33 | entity.queryParameter = jsonObject.optString("queryParameter") 34 | entity.requestBody = jsonObject.optString("requestBody") 35 | entity.responseHeader = jsonObject.optString("responseHeader") 36 | entity.responseBody = jsonObject.getString("responseBody") 37 | return entity 38 | } catch (e: Exception) { 39 | e.printStackTrace() 40 | } 41 | 42 | return null 43 | } 44 | } 45 | 46 | fun toJson(): String { 47 | val map = mutableMapOf() 48 | map["path"] = path 49 | map["requestUrl"] = requestUrl 50 | map["requestMethod"] = requestMethod 51 | map["statusCode"] = statusCode 52 | map["requestHeader"] = requestHeader 53 | map["queryParameter"] = queryParameter 54 | map["requestBody"] = requestBody 55 | map["responseHeader"] = responseHeader 56 | map["responseBody"] = responseBody 57 | return JSONObject(map).toString(2) 58 | } 59 | } -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 |