From ceb5abaae35b3094a35f49317979493a64a8c486 Mon Sep 17 00:00:00 2001 From: liujingxing <327744707@qq.com> Date: Wed, 23 Jan 2019 17:48:12 +0800 Subject: [PATCH 001/402] Initial commit --- .gitignore | 65 +++++++++++++++++ LICENSE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 + 3 files changed, 268 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..39b6783c --- /dev/null +++ b/.gitignore @@ -0,0 +1,65 @@ +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +.idea/caches + +# Keystore files +# Uncomment the following line if you do not want to check your keystore files in. +#*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..261eeb9e --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/README.md b/README.md new file mode 100644 index 00000000..90a0428d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# HttpSender +OkHttp+RxJava 超好用的Http请求框架 From a0c268e2dbb23ae5fdc82ce6babe2752ce45e4f8 Mon Sep 17 00:00:00 2001 From: liujingxing <327744707@qq.com> Date: Wed, 23 Jan 2019 18:09:51 +0800 Subject: [PATCH 002/402] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=B7=A5=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 20 ++ app/.gitignore | 1 + app/build.gradle | 33 +++ app/proguard-rules.pro | 21 ++ app/src/main/AndroidManifest.xml | 23 ++ .../com/example/httpsender/DataParser.java | 45 ++++ .../com/example/httpsender/MainActivity.java | 208 +++++++++++++++ .../java/com/example/httpsender/Response.java | 30 +++ .../com/example/httpsender/ResponseType.java | 24 ++ .../java/com/example/httpsender/Update.java | 9 + .../drawable-v24/ic_launcher_foreground.xml | 34 +++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++ app/src/main/res/layout/activity_main.xml | 19 ++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 + .../example/httpsender/ExampleUnitTest.java | 17 ++ build.gradle | 44 ++++ lib_http/build.gradle | 130 ++++++++++ lib_http/proguard-rules.pro | 21 ++ lib_http/src/main/AndroidManifest.xml | 2 + .../src/main/java/httpsender/Function.java | 19 ++ .../src/main/java/httpsender/HttpSender.java | 190 ++++++++++++++ .../java/httpsender/SSLSocketFactoryImpl.java | 150 +++++++++++ lib_http/src/main/java/httpsender/Sender.java | 106 ++++++++ .../java/httpsender/X509TrustManagerImpl.java | 28 ++ .../wrapper/callback/GT0Predicate.java | 18 ++ .../wrapper/callback/ProgressCallback.java | 22 ++ .../wrapper/entity/ParameterizedTypeImpl.java | 36 +++ .../httpsender/wrapper/entity/Progress.java | 64 +++++ .../httpsender/wrapper/entity/UpFile.java | 38 +++ .../exception/EmptyObjectException.java | 21 ++ .../wrapper/exception/ExceptionHelper.java | 39 +++ .../wrapper/exception/FailException.java | 20 ++ .../exception/NetworkErrorException.java | 18 ++ .../wrapper/exception/ParseException.java | 45 ++++ .../wrapper/param/AbstractDeleteParam.java | 24 ++ .../wrapper/param/AbstractParam.java | 118 +++++++++ .../wrapper/param/AbstractPatchParam.java | 24 ++ .../wrapper/param/AbstractPostParam.java | 24 ++ .../wrapper/param/AbstractPutParam.java | 24 ++ .../httpsender/wrapper/param/BodyRequest.java | 12 + .../wrapper/param/DeleteFormParam.java | 28 ++ .../wrapper/param/DeleteJsonParam.java | 28 ++ .../wrapper/param/DeleteRequest.java | 10 + .../httpsender/wrapper/param/GetParam.java | 28 ++ .../httpsender/wrapper/param/GetRequest.java | 12 + .../httpsender/wrapper/param/HeadParam.java | 28 ++ .../httpsender/wrapper/param/HeadRequest.java | 12 + .../wrapper/param/HeadersBuilder.java | 27 ++ .../wrapper/param/NoBodyRequest.java | 38 +++ .../java/httpsender/wrapper/param/Param.java | 114 +++++++++ .../wrapper/param/ParamBuilder.java | 63 +++++ .../wrapper/param/PatchFormParam.java | 28 ++ .../wrapper/param/PatchJsonParam.java | 28 ++ .../wrapper/param/PatchRequest.java | 10 + .../wrapper/param/PostFormParam.java | 63 +++++ .../wrapper/param/PostJsonParam.java | 28 ++ .../httpsender/wrapper/param/PostRequest.java | 10 + .../wrapper/param/ProgressParam.java | 19 ++ .../wrapper/param/PutFormParam.java | 28 ++ .../wrapper/param/PutJsonParam.java | 28 ++ .../httpsender/wrapper/param/PutRequest.java | 10 + .../wrapper/param/RequestBodyBuilder.java | 13 + .../wrapper/param/RequestBuilder.java | 13 + .../wrapper/parse/AbstractParser.java | 63 +++++ .../wrapper/parse/DownloadParser.java | 44 ++++ .../java/httpsender/wrapper/parse/Parser.java | 17 ++ .../wrapper/parse/SimpleParser.java | 33 +++ .../wrapper/parse/StringParser.java | 23 ++ .../wrapper/progress/ProgressInterceptor.java | 32 +++ .../wrapper/progress/ProgressRequestBody.java | 120 +++++++++ .../progress/ProgressResponseBody.java | 113 +++++++++ .../httpsender/wrapper/utils/BuildUtil.java | 239 ++++++++++++++++++ .../httpsender/wrapper/utils/GsonUtil.java | 122 +++++++++ .../java/httpsender/wrapper/utils/IOUtil.java | 150 +++++++++++ lib_http/src/main/res/values/strings.xml | 2 + settings.gradle | 1 + 90 files changed, 3546 insertions(+) create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/example/httpsender/DataParser.java create mode 100644 app/src/main/java/com/example/httpsender/MainActivity.java create mode 100644 app/src/main/java/com/example/httpsender/Response.java create mode 100644 app/src/main/java/com/example/httpsender/ResponseType.java create mode 100644 app/src/main/java/com/example/httpsender/Update.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/example/httpsender/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 lib_http/build.gradle create mode 100644 lib_http/proguard-rules.pro create mode 100644 lib_http/src/main/AndroidManifest.xml create mode 100644 lib_http/src/main/java/httpsender/Function.java create mode 100644 lib_http/src/main/java/httpsender/HttpSender.java create mode 100644 lib_http/src/main/java/httpsender/SSLSocketFactoryImpl.java create mode 100644 lib_http/src/main/java/httpsender/Sender.java create mode 100644 lib_http/src/main/java/httpsender/X509TrustManagerImpl.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/callback/GT0Predicate.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/callback/ProgressCallback.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/entity/ParameterizedTypeImpl.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/entity/Progress.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/entity/UpFile.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/exception/EmptyObjectException.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/exception/ExceptionHelper.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/exception/FailException.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/exception/NetworkErrorException.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/exception/ParseException.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/AbstractDeleteParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/AbstractParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/AbstractPatchParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/AbstractPostParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/AbstractPutParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/BodyRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/DeleteFormParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/DeleteJsonParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/DeleteRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/GetParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/GetRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/HeadParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/HeadRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/HeadersBuilder.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/NoBodyRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/Param.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/ParamBuilder.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PatchFormParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PatchJsonParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PatchRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PostFormParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PostJsonParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PostRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/ProgressParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PutFormParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PutJsonParam.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/PutRequest.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/RequestBodyBuilder.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/param/RequestBuilder.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/parse/AbstractParser.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/parse/DownloadParser.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/parse/Parser.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/parse/SimpleParser.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/parse/StringParser.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/progress/ProgressInterceptor.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/progress/ProgressRequestBody.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/progress/ProgressResponseBody.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/utils/BuildUtil.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/utils/GsonUtil.java create mode 100644 lib_http/src/main/java/httpsender/wrapper/utils/IOUtil.java create mode 100644 lib_http/src/main/res/values/strings.xml create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore index 39b6783c..992db669 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,23 @@ fastlane/Preview.html fastlane/screenshots fastlane/test_output fastlane/readme.md + +gradle/wrapper/gradle-wrapper.jar + +*.properties + +.idea/codeStyles/Project.xml + +.idea/misc.xml + +.idea/modules.xml + +.idea/runConfigurations.xml + +.idea/vcs.xml + +app/src/androidTest/java/com/example/httpsender/ExampleInstrumentedTest.java + +gradlew.bat + +gradlew diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 00000000..8d68c802 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "com.example.httpsender" + minSdkVersion 19 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation project(":lib_http") + implementation 'androidx.appcompat:appcompat:1.1.0-alpha01' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 00000000..f1b42451 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..b7f889aa --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/httpsender/DataParser.java b/app/src/main/java/com/example/httpsender/DataParser.java new file mode 100644 index 00000000..12af319a --- /dev/null +++ b/app/src/main/java/com/example/httpsender/DataParser.java @@ -0,0 +1,45 @@ +package com.example.httpsender; + + +import httpsender.wrapper.exception.*; +import httpsender.wrapper.parse.*; + +import java.io.IOException; +import java.lang.reflect.Type; + +import httpsender.wrapper.utils.GsonUtil; + +/** + * 数据解析器 + * User: ljx + * Date: 2018/10/23 + * Time: 13:49 + */ +public class DataParser extends AbstractParser { + + protected DataParser() { + super(); + } + + @SuppressWarnings("unchecked") + @Override + public T onParse(okhttp3.Response response) throws IOException { + String content = getResult(response); + final Type type = getActualTypeParameter(); + Response hr = GsonUtil.getObject(content, ResponseType.get(type)); + throwIfFatal(hr); + T t = hr.getData(); + if (t == null) { + if (type == String.class) return (T) hr.getMsg(); + throw new EmptyObjectException(String.valueOf(hr.getCode()), hr.getMsg()); + } + return t; + } + + private void throwIfFatal(Response response) throws IOException { + if (response == null) + throw new ParseException("data parse error"); + if (!response.isSuccess()) + throw new FailException(String.valueOf(response.getCode()), response.getMsg()); + } +} diff --git a/app/src/main/java/com/example/httpsender/MainActivity.java b/app/src/main/java/com/example/httpsender/MainActivity.java new file mode 100644 index 00000000..428b6b5c --- /dev/null +++ b/app/src/main/java/com/example/httpsender/MainActivity.java @@ -0,0 +1,208 @@ +package com.example.httpsender; + +import android.os.Bundle; +import android.util.Log; +import android.view.View; + +import java.io.File; + +import androidx.appcompat.app.AppCompatActivity; +import httpsender.HttpSender; +import httpsender.wrapper.entity.Progress; +import httpsender.wrapper.param.Param; +import httpsender.wrapper.parse.DownloadParser; +import httpsender.wrapper.parse.SimpleParser; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; +import io.reactivex.functions.Consumer; +import io.reactivex.functions.Function; +import io.reactivex.functions.Predicate; +import io.reactivex.plugins.RxJavaPlugins; + +public class MainActivity extends AppCompatActivity { + public static final String url = "http://update.9158.com/miaolive/Miaolive.apk"; + public static final String url1 = "http://update.9158.com/miaolive/Miaolive.apk?"; + public static final String url2 = "http://update.9158.com/miaolive/Miaolive.apk?version=100"; + Disposable subscribe; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + String url = "http://www.baidu.com"; + Param param = Param.postForm(url) //FormParam 表明发送表单形式的Post请求,后续会继续介绍 + .add("versionCode", 100) + .add("versionName", "1.0.0") + .addHeader("deviceType", "android") + .add("file", new File("xxx/1.png"))//new 一个File对象并传入文件路径即可上传文件 + .add("file1", new File("xxx/2.png")); + Disposable disposable = HttpSender + .from(param, new SimpleParser() {}) //注意:这里使用from操作符 + .observeOn(AndroidSchedulers.mainThread()) //主线程回调 + .subscribe(new Consumer() { + @Override + public void accept(Update update) throws Exception { + //这里拿到Http执行结果,默认为String类 + } + }, new Consumer() { + @Override + public void accept(Throwable throwable) throws Exception { + //出现错误 + } + }); + + } + + //HttpSender初始化 + private void HttpSenderInit() { +// HttpSender.init(new OkHttpClient());//自定义OkHttpClient对象 + RxJavaPlugins.setErrorHandler(throwable -> { + }); + HttpSender.setOnParamAssembly(p -> { + //这里添加公共参数 + return p; + }); + } + + public void onClick(View view) { + sendGet(); + } + + private void sendGet() { + String url = "http://ip.taobao.com/service/getIpInfo.php"; + Param param = Param.postForm(url) + .add("ip", "63.223.108.42") + .addHeader("accept", "*/*") + .addHeader("connection", "Keep-Alive") + .addHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); + Disposable subscribe = HttpSender.from(param, new SimpleParser>() {}) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(s -> { + Log.e("LJX", "accept s=" + s.getData()); + }, throwable -> { + Log.e("LJX", "accept throwable=" + throwable.getMessage()); + }); + } + + //下载文件,不带进度 + private void download() { + String url = "http://update.9158.com/miaolive/Miaolive.apk"; + Param param = Param.get(url); //这里可添加参数及请求头信息 + //文件存储路径 + String destPath = getExternalCacheDir() + "/" + System.currentTimeMillis() + ".apk"; + Disposable disposable = HttpSender + .from(param, new DownloadParser(destPath)) //注意这里使用DownloadParser解析器,并传入本地路径 + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(s -> { + //下载成功,处理相关逻辑 + }, throwable -> { + //下载失败,处理相关逻辑 + }); + } + + //下载文件,带进度 + private void downloadAndProgress() { + String url = "http://update.9158.com/miaolive/Miaolive.apk"; + //文件存储路径 + String destPath = getExternalCacheDir() + "/" + System.currentTimeMillis() + ".apk"; + Param param = Param.get(url); //这里可添加参数及请求头信息 + Disposable disposable = HttpSender + .download(param, destPath) //注意这里使用download操作符 + .observeOn(AndroidSchedulers.mainThread()) + .doOnNext(progress -> { + //下载进度更新,0-100,仅在进度有更新时才会回调 + int currentProgress = progress.getProgress(); //当前进度 0-100 + long currentSize = progress.getCurrentSize(); //当前已下载的字节大小 + long totalSize = progress.getTotalSize(); //要下载的总字节大小 + }) + .filter(Progress::isCompleted)//下载完成,才继续往下走 + .map(Progress::getResult) //到这,说明下载完成,返回下载目标路径 + .subscribe(s -> { + //下载完成,处理相关逻辑 + }, throwable -> { + //下载失败,处理相关逻辑 + }); + } + + //上传文件,带进度 + private void uploadAndProgress() { + String url = "http://www.baidu.com"; + Param param = Param.postForm(url) //FormParam 表明发送表单形式的Post请求,后续会继续介绍 + .add("versionCode", 100) + .add("versionName", "1.0.0") + .addHeader("deviceType", "android") + .add("file", new File("xxx/1.png"))//new 一个File对象并传入文件路径即可上传文件 + .add("file1", new File("xxx/2.png")); + Disposable disposable = HttpSender + //注意这里使用upload操作符 + .upload(param, new SimpleParser() {}) + .observeOn(AndroidSchedulers.mainThread()) + .doOnNext(new Consumer>() { + @Override + public void accept(Progress progress) throws Exception { + //上传进度更新,0-100,仅在进度有更新时才会回调 + int currentProgress = progress.getProgress(); //当前进度 0-100 + long currentSize = progress.getCurrentSize(); //当前已上传的字节大小 + long totalSize = progress.getTotalSize(); //要上传的总字节大小 + } + }) + .filter(new Predicate>() { + @Override + public boolean test(Progress progress) throws Exception { + //过滤事件,上传完成,才继续往下走 + return progress.isCompleted(); + } + }) + .map(new Function, Update>() { + @Override + public Update apply(Progress progress) throws Exception { + //返回Http执行结果 + return progress.getResult(); + } + }) + .subscribe(new Consumer() { + @Override + public void accept(Update update) throws Exception { + //上传成功 + } + }, new Consumer() { + @Override + public void accept(Throwable throwable) throws Exception { + //上传失败 + } + }); + } + + + //上传文件,带进度 + private void uploadAndProgress1() { + String url = "http://www.baidu.com"; + Param param = Param.postForm(url) //FormParam 表明发送表单形式的Post请求,后续会继续介绍 + .add("versionCode", 100) + .add("versionName", "1.0.0") + .addHeader("deviceType", "android") + .add("file", new File("xxx/1.png"))//new 一个File对象并传入文件路径即可上传文件 + .add("file1", new File("xxx/2.png")); + //过滤事件,上传完成,才继续往下走 + //返回Http执行结果 + Disposable disposable = HttpSender + //注意这里使用upload操作符 + .upload(param, new SimpleParser() {}) + .observeOn(AndroidSchedulers.mainThread()) + .doOnNext(progress -> { + //上传进度更新,0-100,仅在进度有更新时才会回调 + int currentProgress = progress.getProgress(); //当前进度 0-100 + long currentSize = progress.getCurrentSize(); //当前已上传的字节大小 + long totalSize = progress.getTotalSize(); //要上传的总字节大小 + }) + .filter(Progress::isCompleted) //过滤事件,上传完成,才继续往下走 + .map(Progress::getResult) //返回Http执行结果 + .subscribe(update -> { + //上传成功 + }, throwable -> { + //上传失败 + }); + } + +} diff --git a/app/src/main/java/com/example/httpsender/Response.java b/app/src/main/java/com/example/httpsender/Response.java new file mode 100644 index 00000000..d304c181 --- /dev/null +++ b/app/src/main/java/com/example/httpsender/Response.java @@ -0,0 +1,30 @@ +package com.example.httpsender; + + +/** + * User: ljx + * Date: 2018/10/21 + * Time: 13:16 + */ +public class Response { + + private int code; + private String msg; + private T data; + + public int getCode() { + return code; + } + + public String getMsg() { + return msg == null ? "" : msg; + } + + public T getData() { + return data; + } + + public boolean isSuccess() { + return true; + } +} diff --git a/app/src/main/java/com/example/httpsender/ResponseType.java b/app/src/main/java/com/example/httpsender/ResponseType.java new file mode 100644 index 00000000..956d94aa --- /dev/null +++ b/app/src/main/java/com/example/httpsender/ResponseType.java @@ -0,0 +1,24 @@ +package com.example.httpsender; + + + +import java.lang.reflect.Type; + +import httpsender.wrapper.entity.ParameterizedTypeImpl; + +/** + * User: ljx + * Date: 2018/10/23 + * Time: 09:36 + */ +public class ResponseType extends ParameterizedTypeImpl { + + private ResponseType(Type actualType) { + super(new Type[]{actualType}, null, Response.class); + } + + public static ResponseType get(Type actualType) { + return new ResponseType(actualType); + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/httpsender/Update.java b/app/src/main/java/com/example/httpsender/Update.java new file mode 100644 index 00000000..cf6421bd --- /dev/null +++ b/app/src/main/java/com/example/httpsender/Update.java @@ -0,0 +1,9 @@ +package com.example.httpsender; + +/** + * User: ljx + * Date: 2019/1/22 + * Time: 20:50 + */ +public class Update { +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..1f6bb290 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..0d025f9b --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..6021781d --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,19 @@ + + + +