├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ └── ic_launcher.png
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ └── styles.xml
│ │ │ ├── values-v21
│ │ │ │ └── styles.xml
│ │ │ ├── values-w820dp
│ │ │ │ └── dimens.xml
│ │ │ ├── menu
│ │ │ │ └── menu_main.xml
│ │ │ └── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── content_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── im
│ │ │ └── wangchao
│ │ │ └── mhttpdemo
│ │ │ ├── SampleApi.java
│ │ │ ├── SampleDefaultApi.java
│ │ │ ├── GetExample.java
│ │ │ ├── PostExample.java
│ │ │ ├── Cache.java
│ │ │ ├── MainActivity.java
│ │ │ └── Android5SSL.java
│ ├── androidTest
│ │ └── java
│ │ │ └── im
│ │ │ └── wangchao
│ │ │ └── mhttpdemo
│ │ │ ├── ApplicationTest.java
│ │ │ └── ExampleInstrumentedTest.java
│ └── test
│ │ └── java
│ │ └── im
│ │ └── wangchao
│ │ └── mhttp
│ │ └── ExampleUnitTest.java
├── proguard-rules.pro
└── build.gradle
├── mhttp
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── im
│ │ └── wangchao
│ │ └── mhttp
│ │ ├── Converter.java
│ │ ├── internal
│ │ ├── exception
│ │ │ ├── ParserException.java
│ │ │ └── ResponseFailException.java
│ │ ├── Version.java
│ │ ├── Singleton.java
│ │ ├── cookie
│ │ │ ├── ClearableCookieJar.java
│ │ │ ├── cache
│ │ │ │ ├── CookieCache.java
│ │ │ │ ├── SetCookieCache.java
│ │ │ │ └── IdentifiableCookie.java
│ │ │ ├── persistence
│ │ │ │ ├── CookiePersistor.java
│ │ │ │ ├── SharedPrefsCookiePersistor.java
│ │ │ │ └── SerializableCookie.java
│ │ │ ├── MemoryCookieJar.java
│ │ │ └── PersistentCookieJar.java
│ │ ├── log
│ │ │ └── LoggerImpl.java
│ │ └── interceptor
│ │ │ └── MBridgeInterceptor.java
│ │ ├── URLInterceptor.java
│ │ ├── executor
│ │ ├── BACKGROUND.java
│ │ ├── MAIN.java
│ │ └── SENDING.java
│ │ ├── Method.java
│ │ ├── BindApi.java
│ │ ├── callback
│ │ ├── BitmapCallbackHandler.java
│ │ ├── TextCallbackHandler.java
│ │ ├── BinaryCallbackHandler.java
│ │ ├── GSONCallbackHandler.java
│ │ ├── JSONCallbackHandler.java
│ │ └── FileCallbackHandler.java
│ │ ├── Callback.java
│ │ ├── Accept.java
│ │ ├── ThreadMode.java
│ │ ├── body
│ │ ├── FileBody.java
│ │ ├── JSONBody.java
│ │ ├── MediaTypeUtils.java
│ │ ├── ProgressRequestBody.java
│ │ └── OctetStreamBody.java
│ │ ├── Response.java
│ │ ├── rxjava2
│ │ ├── ResponseExecuteObservable.java
│ │ ├── ResponseEnqueueObservable.java
│ │ └── RxRequest.java
│ │ ├── Android5SSL.java
│ │ ├── HTTPS.java
│ │ ├── MHttp.java
│ │ └── AbsCallbackHandler.java
├── gradle.properties
├── build.gradle
├── proguard-rules.pro
└── mhttp.iml
├── mhttp-adapter
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── im
│ │ │ └── wangchao
│ │ │ └── mhttp
│ │ │ └── adapter
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── im
│ │ └── wangchao
│ │ └── mhttp
│ │ └── adapter
│ │ └── ApplicationTest.java
├── proguard-rules.pro
├── build.gradle
└── mhttp-adapter.iml
├── mhttp-compiler
├── .gitignore
├── src
│ ├── main
│ │ ├── resources
│ │ │ └── META-INF
│ │ │ │ └── services
│ │ │ │ └── javax.annotation.processing.Processor
│ │ └── java
│ │ │ └── im
│ │ │ └── wangchao
│ │ │ └── http
│ │ │ └── compiler
│ │ │ ├── MyClass.java
│ │ │ ├── BindClass.java
│ │ │ └── BindMethod.java
│ └── test
│ │ └── java
│ │ └── im
│ │ └── wangchao
│ │ └── http
│ │ └── compiler
│ │ └── JavaPoetTest.java
├── build.gradle
└── gradle.properties
├── mhttp-annotations
├── .gitignore
├── build.gradle
├── src
│ └── main
│ │ └── java
│ │ └── im
│ │ └── wangchao
│ │ └── http
│ │ └── annotations
│ │ ├── Tag.java
│ │ ├── Callback.java
│ │ ├── Timeout.java
│ │ ├── RootURL.java
│ │ ├── CommonParamsMethod.java
│ │ ├── Header.java
│ │ ├── RequestContentType.java
│ │ ├── Get.java
│ │ └── Post.java
└── gradle.properties
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── upload.sh
├── makeProcessor.sh
├── gradle.properties
├── MHttp.iml
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/mhttp/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
--------------------------------------------------------------------------------
/mhttp-adapter/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/mhttp-compiler/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
3 |
--------------------------------------------------------------------------------
/mhttp-annotations/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 | *.iml
3 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':mhttp', ':mhttp-compiler', ':mhttp-annotations', ':mhttp-adapter'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jymot/MHttp/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/mhttp-adapter/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
Description : Converter.
5 | *Author : wangchao.
6 | *Date : 2018/3/20.
7 | *Time : 下午10:26.
8 | */ 9 | public interface ConverterDescription : ParserException.
5 | *Author : wangchao.
6 | *Date : 16/4/26.
7 | *Time : 下午8:38.
8 | */ 9 | public class ParserException extends Exception{ 10 | public ParserException(){ 11 | super("Response parse exception."); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /app/src/androidTest/java/im/wangchao/mhttpdemo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttpdemo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCaseDescription : ResponseFailException.
5 | *Author : wangchao.
6 | *Date : 16/4/25.
7 | *Time : 下午4:06.
8 | */ 9 | public class ResponseFailException extends Exception{ 10 | //Response Non Successful 11 | public ResponseFailException(){ 12 | super("Response failure exception."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/URLInterceptor.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp; 2 | 3 | import java.net.URL; 4 | 5 | import okhttp3.HttpUrl; 6 | 7 | /** 8 | *Description : URLInterceptor.
9 | *Author : wangchao.
10 | *Date : 2018/1/15.
11 | *Time : 上午11:01.
12 | */ 13 | public interface URLInterceptor { 14 | 15 | String interceptor(String origin); 16 | 17 | HttpUrl interceptor(HttpUrl origin); 18 | 19 | URL interceptor(URL origin); 20 | } 21 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/executor/BACKGROUND.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.executor; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import java.util.concurrent.Executor; 6 | 7 | /** 8 | *Description : BACKGROUND.
9 | *Author : wangchao.
10 | *Date : 16/9/2.
11 | *Time : 下午4:50.
12 | */ 13 | public final class BACKGROUND implements Executor { 14 | @Override public void execute(@NonNull Runnable command) { 15 | command.run(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/Tag.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | *Description : Tag.
10 | * 11 | *Author : wangchao.
12 | *Date : 16/3/22.
13 | *Time : 上午10:58.
14 | */ 15 | @Retention(RetentionPolicy.CLASS) 16 | @Target(ElementType.PARAMETER) 17 | public @interface Tag { 18 | } 19 | -------------------------------------------------------------------------------- /mhttp-compiler/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | sourceCompatibility = rootProject.ext.sourceCompatibilityVersion 4 | targetCompatibility = rootProject.ext.targetCompatibilityVersion 5 | 6 | dependencies { 7 | implementation deps.mhttpannotations 8 | // implementation project(':mhttp-annotations') 9 | implementation deps.autoservice 10 | implementation deps.autocommon 11 | implementation deps.javapoet 12 | 13 | testImplementation deps.junit 14 | } 15 | 16 | //apply from: 'https://raw.githubusercontent.com/motcwang/Utils/master/bintray-publish/bintray-java.gradle' -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/Callback.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | *Description : Callback.
10 | * 11 | *Author : wangchao.
12 | *Date : 15/10/17.
13 | *Time : 下午6:02.
14 | */ 15 | @Retention(RetentionPolicy.CLASS) 16 | @Target(ElementType.PARAMETER) 17 | public @interface Callback { 18 | } 19 | -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/Timeout.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | *Description : Timeout.废弃,不处理该 Annotation
10 | * 11 | *Author : wangchao.
12 | *Date : 15/10/31.
13 | *Time : 下午1:13.
14 | */ 15 | @Retention(RetentionPolicy.SOURCE) 16 | @Target(ElementType.FIELD) 17 | @Deprecated 18 | public @interface Timeout { 19 | int value(); 20 | } 21 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/internal/Version.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.internal; 2 | 3 | import im.wangchao.mhttp.BuildConfig; 4 | 5 | /** 6 | *Description : Version.
7 | *Author : wangchao.
8 | *Date : 16/8/24.
9 | *Time : 下午2:47.
10 | */ 11 | public class Version { 12 | private Version(){ 13 | throw new AssertionError(); 14 | } 15 | 16 | public static String userAgent() { 17 | return moduleName().concat(BuildConfig.VERSION_NAME); 18 | } 19 | 20 | public static String moduleName() { 21 | return "mhttp"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/RootURL.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.Target; 5 | 6 | import static java.lang.annotation.ElementType.FIELD; 7 | import static java.lang.annotation.RetentionPolicy.CLASS; 8 | 9 | /** 10 | *Description : RootURL.
11 | * 12 | *Author : wangchao.
13 | *Date : 15/10/31.
14 | *Time : 上午10:17.
15 | */ 16 | @Retention(CLASS) 17 | @Target(FIELD) 18 | public @interface RootURL { 19 | /** 20 | * request root address 21 | */ 22 | String value(); 23 | } 24 | -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/CommonParamsMethod.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | *Description : CommonParamsMethod. 10 | * method name will return Map(String, Object)
11 | *Author : wangchao.
12 | *Date : 15/11/2.
13 | *Time : 下午8:23.
14 | */ 15 | @Retention(RetentionPolicy.SOURCE) 16 | @Target(ElementType.METHOD) 17 | public @interface CommonParamsMethod { 18 | } 19 | -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/Header.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | *Description : Header.
10 | * 11 | *Author : wangchao.
12 | *Date : 15/10/31.
13 | *Time : 下午1:17.
14 | */ 15 | @Retention(RetentionPolicy.SOURCE) 16 | @Target(ElementType.FIELD) 17 | public @interface Header { 18 | /** 19 | * the common header of request 20 | */ 21 | String[] value(); 22 | } 23 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/executor/MAIN.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.executor; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | import android.support.annotation.NonNull; 6 | 7 | import java.util.concurrent.Executor; 8 | 9 | /** 10 | *Description : MainThread.
11 | *Author : wangchao.
12 | *Date : 16/9/2.
13 | *Time : 下午4:45.
14 | */ 15 | public final class MAIN implements Executor{ 16 | private final Handler handler = new Handler(Looper.getMainLooper()); 17 | 18 | @Override public void execute(@NonNull Runnable command) { 19 | handler.post(command); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/internal/Singleton.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.internal; 2 | 3 | /** 4 | *Description : Singleton.
5 | *Author : wangchao.
6 | *Date : 16/8/25.
7 | *Time : 上午10:37.
8 | */ 9 | public abstract class SingletonDescription : RequestContentType.
11 | * 12 | *Author : wangchao.
13 | *Date : 15/10/31.
14 | *Time : 下午1:09.
15 | */ 16 | @Retention(CLASS) 17 | @Target(FIELD) 18 | public @interface RequestContentType { 19 | /** 20 | * request Content-Type 21 | */ 22 | String value(); 23 | } 24 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wangchao/Work/android-sdk/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /mhttp-adapter/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wangchao/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 |Description : Method.
10 | *Author : wangchao.
11 | *Date : 16/3/8.
12 | *Time : 下午4:08.
13 | */ 14 | public interface Method { 15 | String POST = "POST"; 16 | String GET = "GET"; 17 | String HEAD = "HEAD"; 18 | String DELETE = "DELETE"; 19 | String PUT = "PUT"; 20 | String PATCH = "PATCH"; 21 | 22 | @Retention(RetentionPolicy.SOURCE) 23 | @StringDef({Method.POST, Method.GET, Method.HEAD, Method.DELETE, Method.PUT, Method.PATCH}) 24 | @interface MethodType{} 25 | } 26 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/BindApi.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp; 2 | 3 | 4 | import android.util.Log; 5 | 6 | /** 7 | *Description : BindApi.
8 | *Author : wangchao.
9 | *Date : 15/10/19.
10 | *Time : 上午8:23.
11 | */ 12 | final class BindApi { 13 | /** HttpProcessor.SUFFIX */ 14 | private static final String SUFFIX = "_HttpBinder"; 15 | 16 | @SuppressWarnings("unchecked") public staticDescription : ImageResponseHandler.
11 | *Author : wangchao.
12 | *Date : 15/10/18.
13 | *Time : 下午2:49.
14 | */ 15 | public abstract class BitmapCallbackHandler extends AbsCallbackHandlerDescription : Get.
10 | * 11 | *Author : wangchao.
12 | *Date : 15/10/17.
13 | *Time : 下午6:38.
14 | */ 15 | @Retention(RetentionPolicy.CLASS) 16 | @Target(ElementType.METHOD) 17 | public @interface Get { 18 | 19 | /** 20 | * the request url 21 | */ 22 | String url(); 23 | 24 | /** 25 | * the request tag 26 | */ 27 | String tag() default ""; 28 | 29 | @Deprecated 30 | int timeout() default 0; 31 | 32 | /** 33 | * request heads 34 | */ 35 | String[] heads() default {}; 36 | } 37 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/executor/SENDING.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.executor; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | import android.support.annotation.NonNull; 6 | 7 | import java.util.concurrent.Executor; 8 | 9 | /** 10 | *Description : SENDING.
11 | *Author : wangchao.
12 | *Date : 16/9/2.
13 | *Time : 下午4:47.
14 | */ 15 | public final class SENDING implements Executor { 16 | private final Handler handler; 17 | public SENDING(){ 18 | if (Looper.myLooper() == null){ 19 | throw new RuntimeException("The Looper of the current thread is null, please call Looper.prepare() on your thread."); 20 | } 21 | handler = new Handler(Looper.myLooper()); 22 | } 23 | 24 | @Override public void execute(@NonNull Runnable command) { 25 | handler.post(command); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/internal/cookie/ClearableCookieJar.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Francisco José Montiel Navarro. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package im.wangchao.mhttp.internal.cookie; 18 | 19 | import okhttp3.CookieJar; 20 | 21 | public interface ClearableCookieJar extends CookieJar { 22 | 23 | void clear(); 24 | } 25 | -------------------------------------------------------------------------------- /mhttp-annotations/src/main/java/im/wangchao/http/annotations/Post.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.annotations; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | /** 9 | *Description : Post.
10 | * 11 | *Author : wangchao.
12 | *Date : 15/10/17.
13 | *Time : 下午5:55.
14 | */ 15 | @Retention(RetentionPolicy.CLASS) 16 | @Target(ElementType.METHOD) 17 | public @interface Post { 18 | /** 19 | * the request url 20 | */ 21 | String url(); 22 | 23 | /** 24 | * the request tag 25 | */ 26 | String tag() default ""; 27 | 28 | /** 29 | * timeout 30 | */ 31 | @Deprecated 32 | int timeout() default 0; 33 | 34 | /** 35 | * request heads 36 | */ 37 | String[] heads() default {}; 38 | } 39 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/callback/TextCallbackHandler.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.callback; 2 | 3 | import im.wangchao.mhttp.AbsCallbackHandler; 4 | import im.wangchao.mhttp.Accept; 5 | import im.wangchao.mhttp.Response; 6 | 7 | /** 8 | *Description : TextResponseHandler.
9 | *Author : wangchao.
10 | *Date : 15/10/18.
11 | *Time : 下午2:41.
12 | */ 13 | public class TextCallbackHandler extends AbsCallbackHandlerDescription : BinaryResponseHandler.
9 | *Author : wangchao.
10 | *Date : 15/10/18.
11 | *Time : 下午2:49.
12 | */ 13 | public class BinaryCallbackHandler extends AbsCallbackHandlerDescription : OkCallback.
10 | *Author : wangchao.
11 | *Date : 16/6/3.
12 | *Time : 上午10:16.
13 | */ 14 | public interface Callback extends okhttp3.Callback { 15 | 16 | Callback EMPTY = new Callback() { 17 | @Override public void initialize(Request request) {} 18 | 19 | @Override public String accept() { 20 | return Accept.EMPTY; 21 | } 22 | 23 | @Override public void onFailure(Call call, IOException e) {} 24 | 25 | @Override public void onResponse(Call call, Response response) throws IOException { 26 | response.close(); 27 | } 28 | }; 29 | 30 | /** 31 | * Initialize the callback. 32 | */ 33 | void initialize(Request request); 34 | 35 | /** 36 | * Request accept. 37 | */ 38 | String accept(); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/Accept.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp; 2 | 3 | import android.support.annotation.StringDef; 4 | 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | /** 9 | *Description : Accept.
10 | *Author : wangchao.
11 | *Date : 16/4/25.
12 | *Time : 上午11:06.
13 | */ 14 | public interface Accept { 15 | String EMPTY = ""; 16 | String ACCEPT_JSON = "application/json;charset=utf-8"; 17 | String ACCEPT_TEXT = "text/html;charset=utf-8"; 18 | String ACCEPT_DATA = "application/octet-stream"; 19 | String ACCEPT_IMAGE = "image/png,image/jpeg,image/*"; 20 | String ACCEPT_FILE = "application/octet-stream"; 21 | 22 | @Retention(RetentionPolicy.SOURCE) 23 | @StringDef({ 24 | Accept.EMPTY, 25 | Accept.ACCEPT_JSON, 26 | Accept.ACCEPT_TEXT, 27 | Accept.ACCEPT_DATA, 28 | Accept.ACCEPT_IMAGE, 29 | Accept.ACCEPT_FILE 30 | }) 31 | public @interface $Accept { 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/im/wangchao/mhttpdemo/SampleApi.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttpdemo; 2 | 3 | import im.wangchao.http.annotations.Callback; 4 | import im.wangchao.http.annotations.Get; 5 | import im.wangchao.http.annotations.Tag; 6 | import im.wangchao.mhttp.MHttp; 7 | import im.wangchao.mhttp.Request; 8 | import im.wangchao.mhttp.callback.TextCallbackHandler; 9 | 10 | /** 11 | *Description : SampleApi.
12 | * 13 | *Author : wangchao.
14 | *Date : 15/12/4.
15 | *Time : 下午5:56.
16 | */ 17 | public abstract class SampleApi extends SampleDefaultApi{ 18 | public static SampleApi instance() { 19 | return MHttp.create(SampleApi.class); 20 | } 21 | 22 | @Get(url = "https://www.baidu.com/") 23 | public abstract void baidu(@Callback TextCallbackHandler callback); 24 | 25 | @Get(url = "s", tag = "aaa") 26 | public abstract Request search(String wd, @Callback TextCallbackHandler callback); 27 | 28 | @Get(url = "s", tag = "aaa") 29 | public abstract Request search1(String wd, @Callback TextCallbackHandler callback, @Tag Object a); 30 | } 31 | -------------------------------------------------------------------------------- /mhttp-compiler/src/main/java/im/wangchao/http/compiler/MyClass.java: -------------------------------------------------------------------------------- 1 | //package im.wangchao.http; 2 | // 3 | //import com.squareup.javapoet.JavaFile; 4 | //import com.squareup.javapoet.MethodSpec; 5 | //import com.squareup.javapoet.TypeSpec; 6 | // 7 | //import java.io.IOException; 8 | // 9 | //import javax.lang.model.element.Modifier; 10 | // 11 | //public class MyClass { 12 | // public void testWrite(){ 13 | // MethodSpec method = MethodSpec.methodBuilder("test") 14 | // .addModifiers(Modifier.PUBLIC) 15 | // .returns(void.class) 16 | // .addParameter(String.class, "str") 17 | // .addStatement("$T.out.println(str)", System.class).build(); 18 | // 19 | // TypeSpec typeSpec = TypeSpec.classBuilder("HelloWorld") 20 | // .addModifiers(Modifier.PUBLIC) 21 | // .addMethod(method) 22 | // .build(); 23 | // 24 | // JavaFile javaFile = JavaFile.builder("", typeSpec).build(); 25 | // try { 26 | // javaFile.writeTo(System.out); 27 | // } catch (IOException e) { 28 | // e.printStackTrace(); 29 | // } 30 | // } 31 | //} 32 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/callback/GSONCallbackHandler.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.callback; 2 | 3 | import im.wangchao.mhttp.Response; 4 | import im.wangchao.mhttp.internal.exception.ParserException; 5 | 6 | /** 7 | *Description : GSONResponseHandler.
8 | *Author : wangchao.
9 | *Date : 16/3/20.
10 | *Time : 上午9:06.
11 | */ 12 | public abstract class GSONCallbackHandlerDescription : SampleDefaultApi.
16 | * 17 | *Author : wangchao.
18 | *Date : 15/12/4.
19 | *Time : 下午5:54.
20 | */ 21 | public abstract class SampleDefaultApi { 22 | 23 | @RootURL("https://www.baidu.com/") String baseURL; 24 | @Timeout(40) String timeout; 25 | @RequestContentType(APPLICATION_JSON) String Content_Type; 26 | @Header("Android") String User_Agent; 27 | 28 | @CommonParamsMethod 29 | public MapDescription : Logger.
11 | *Author : wangchao.
12 | *Date : 2018/3/20.
13 | *Time : 下午5:36.
14 | */ 15 | public class LoggerImpl implements HttpLoggingInterceptor.Logger{ 16 | private static final String TAG = Version.moduleName(); 17 | private HttpLoggingInterceptor.Level mLevel; 18 | 19 | public static SingletonDescription : ThreadMode.
11 | *Author : wangchao.
12 | *Date : 16/8/3.
13 | *Time : 下午12:44.
14 | */ 15 | public enum ThreadMode { 16 | /** 17 | * Callback will be called in the same thread, which is sending the request. 18 | */ 19 | SENDING{ 20 | @Override public Executor executor() { 21 | return new SENDING(); 22 | } 23 | }, 24 | /** 25 | * Callback will be called in Android's main thread (UI thread). 26 | */ 27 | MAIN{ 28 | @Override public Executor executor() { 29 | return new MAIN(); 30 | } 31 | }, 32 | 33 | /** 34 | * Callback will be called in a background thread. That is, work on the request thread(okhttp thread). 35 | */ 36 | BACKGROUND{ 37 | @Override public Executor executor() { 38 | return new BACKGROUND(); 39 | } 40 | }; 41 | 42 | public abstract Executor executor(); 43 | } 44 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/internal/interceptor/MBridgeInterceptor.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.internal.interceptor; 2 | 3 | import java.io.IOException; 4 | 5 | import im.wangchao.mhttp.internal.Singleton; 6 | import im.wangchao.mhttp.internal.Version; 7 | import okhttp3.Interceptor; 8 | import okhttp3.Request; 9 | import okhttp3.Response; 10 | 11 | /** 12 | *Description : MBridgeInterceptors.
13 | *Author : wangchao.
14 | *Date : 16/8/24.
15 | *Time : 下午4:32.
16 | */ 17 | public final class MBridgeInterceptor implements Interceptor { 18 | private MBridgeInterceptor(){} 19 | 20 | public static SingletonDescription : JSONResponseHandler.
13 | *Author : wangchao.
14 | *Date : 15/10/18.
15 | *Time : 下午2:25.
16 | */ 17 | public class JSONCallbackHandler extends AbsCallbackHandlerDescription : FileBody.
18 | *Author : wangchao.
19 | *Date : 16/3/8.
20 | *Time : 下午6:10.
21 | */ 22 | public final class FileBody extends RequestBody{ 23 | private final static MediaType CONTENT_TYPE = MediaTypeUtils.OCTET; 24 | private final MediaType contentType; 25 | private final File file; 26 | 27 | public FileBody(File file){ 28 | this(file, null); 29 | } 30 | 31 | public FileBody(File file, String contentType){ 32 | this.file = file; 33 | this.contentType = TextUtils.isEmpty(contentType) ? CONTENT_TYPE : MediaType.parse(contentType); 34 | } 35 | 36 | @Override public MediaType contentType() { 37 | return contentType; 38 | } 39 | 40 | @Override public long contentLength() throws IOException { 41 | return file.length(); 42 | } 43 | 44 | @Override public void writeTo(@NonNull BufferedSink sink) throws IOException { 45 | Source source = null; 46 | try { 47 | source = Okio.source(file); 48 | sink.writeAll(source); 49 | } finally { 50 | Util.closeQuietly(source); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/body/JSONBody.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.body; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.text.TextUtils; 5 | 6 | import java.io.IOException; 7 | 8 | import okhttp3.MediaType; 9 | import okhttp3.RequestBody; 10 | import okio.BufferedSink; 11 | 12 | /** 13 | *Description : JSONBody.
14 | *Author : wangchao.
15 | *Date : 16/3/8.
16 | *Time : 下午4:31.
17 | */ 18 | public final class JSONBody extends RequestBody{ 19 | private final static MediaType CONTENT_TYPE = MediaTypeUtils.JSON; 20 | private final MediaType contentType; 21 | private final byte[] bytes; 22 | 23 | public JSONBody(String content){ 24 | this(content, null); 25 | } 26 | 27 | public JSONBody(String content, String charset){ 28 | if (TextUtils.isEmpty(content)){ 29 | throw new NullPointerException("content == null"); 30 | } 31 | if (TextUtils.isEmpty(charset)){ 32 | charset = "utf-8"; 33 | } 34 | this.contentType = TextUtils.isEmpty(charset) ? CONTENT_TYPE : MediaType.parse("application/json; charset=" + charset); 35 | bytes = content.getBytes(contentType.charset()); 36 | } 37 | 38 | @Override public MediaType contentType() { 39 | return contentType; 40 | } 41 | 42 | @Override public long contentLength() throws IOException { 43 | return bytes.length; 44 | } 45 | 46 | @Override public void writeTo(@NonNull BufferedSink sink) throws IOException { 47 | sink.write(bytes, 0, bytes.length); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /app/src/androidTest/java/im/wangchao/mhttpdemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttpdemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | import android.util.Log; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import im.wangchao.mhttp.Request; 12 | import im.wangchao.mhttp.Response; 13 | import im.wangchao.mhttp.callback.TextCallbackHandler; 14 | 15 | import static org.junit.Assert.assertEquals; 16 | 17 | /** 18 | * Instrumentation test, which will execute on an Android device. 19 | * 20 | * @see Testing documentation 21 | */ 22 | @RunWith(AndroidJUnit4.class) 23 | public class ExampleInstrumentedTest { 24 | @Test 25 | public void useAppContext() throws Exception { 26 | // Context of the app under test. 27 | Context appContext = InstrumentationRegistry.getTargetContext(); 28 | 29 | assertEquals("im.wangchao.mhttpdemo", appContext.getPackageName()); 30 | } 31 | 32 | @Test 33 | public void post() { 34 | Request.builder().post() 35 | .url("https://www.baidu.com") 36 | .callback(new TextCallbackHandler(){ 37 | @Override protected void onSuccess(String data, Response response) { 38 | Log.e("wcwcwc", data); 39 | } 40 | 41 | @Override protected void onFailure(Response response, Throwable throwable) { 42 | Log.e("wcwcwc", "error"); 43 | } 44 | }).build().enqueue(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/internal/cookie/MemoryCookieJar.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.internal.cookie; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import im.wangchao.mhttp.internal.cookie.cache.CookieCache; 8 | import okhttp3.Cookie; 9 | import okhttp3.HttpUrl; 10 | 11 | /** 12 | *Description : MemeryCookieJar.
13 | *Author : wangchao.
14 | *Date : 16/3/18.
15 | *Time : 下午2:20.
16 | */ 17 | public class MemoryCookieJar implements ClearableCookieJar { 18 | 19 | private CookieCache cache; 20 | 21 | public MemoryCookieJar(CookieCache cache) { 22 | this.cache = cache; 23 | } 24 | 25 | @Override 26 | synchronized public void saveFromResponse(HttpUrl url, ListDescription : MResponse.
8 | *Author : wangchao.
9 | *Date : 16/6/3.
10 | *Time : 下午3:03.
11 | */ 12 | public final class Response { 13 | public final static int IO_EXCEPTION_CODE = 1000; 14 | 15 | public static Response error(Request request, int code, String message){ 16 | if (message == null){ 17 | message = "unknown exception."; 18 | } 19 | return new Response(request, new okhttp3.Response.Builder() 20 | .request(request.raw()) 21 | .protocol(Protocol.HTTP_1_1) 22 | .code(code) 23 | .message(message) 24 | .build()); 25 | } 26 | 27 | public static Response newResponse(Request request, okhttp3.Response raw){ 28 | return new Response(request, raw); 29 | } 30 | 31 | private final Request request; 32 | private final okhttp3.Response rawResponse; 33 | 34 | private Response(Request request, okhttp3.Response rawResponse){ 35 | this.request = request; 36 | this.rawResponse = rawResponse; 37 | } 38 | 39 | public Request request() { 40 | return request; 41 | } 42 | 43 | public okhttp3.Response raw() { 44 | return rawResponse; 45 | } 46 | 47 | /** HTTP status code. */ 48 | public int code() { 49 | return rawResponse.code(); 50 | } 51 | 52 | /** HTTP status message or null if unknown. */ 53 | public String message() { 54 | return rawResponse.message(); 55 | } 56 | 57 | /** HTTP headers. */ 58 | public Headers headers() { 59 | return rawResponse.headers(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /mhttp-compiler/src/test/java/im/wangchao/http/compiler/JavaPoetTest.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.http.compiler; 2 | 3 | import com.squareup.javapoet.ClassName; 4 | import com.squareup.javapoet.JavaFile; 5 | import com.squareup.javapoet.MethodSpec; 6 | import com.squareup.javapoet.ParameterizedTypeName; 7 | import com.squareup.javapoet.TypeSpec; 8 | 9 | import org.junit.Test; 10 | 11 | import java.io.IOException; 12 | import java.util.ArrayList; 13 | 14 | import javax.lang.model.element.Modifier; 15 | 16 | /** 17 | *Description : JavaPoetTest.
18 | *Author : wangchao.
19 | *Date : 2017/7/26.
20 | *Time : 下午2:17.
21 | */ 22 | public class JavaPoetTest { 23 | 24 | @Test 25 | public void mainTest(){ 26 | MethodSpec main = MethodSpec.methodBuilder("main") 27 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC) 28 | .returns(void.class) 29 | .addParameter(String[].class, "args") 30 | .addStatement("$T.out.println($S)", System.class, "Hello, JavaPoet!") 31 | .addStatement("$T list = new $T()", ArrayList.class, ArrayList.class) 32 | .build(); 33 | 34 | TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld") 35 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL) 36 | .addMethod(main) 37 | .build(); 38 | 39 | JavaFile javaFile = JavaFile.builder("com.example.helloworld", helloWorld) 40 | .build(); 41 | 42 | try { 43 | javaFile.writeTo(System.out); 44 | } catch (IOException e) { 45 | e.printStackTrace(); 46 | } 47 | 48 | ParameterizedTypeName.get(ClassName.get("im.wangchao.http.compiler", "Test")); 49 | ParameterizedTypeName.get(ClassName.get("im.wangchao.http.compiler", "TT")); 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/body/MediaTypeUtils.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.body; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.text.TextUtils; 5 | 6 | import okhttp3.MediaType; 7 | 8 | /** 9 | *Description : MediaType.
10 | *Author : wangchao.
11 | *Date : 2017/6/23.
12 | *Time : 下午2:15.
13 | */ 14 | public final class MediaTypeUtils { 15 | /** MediaType String */ 16 | public static final String APPLICATION_OCTET_STREAM = "application/octet-stream"; 17 | public static final String APPLICATION_JSON = "application/json; charset=utf-8"; 18 | public static final String APPLICATION_FORM = "application/x-www-form-urlencoded"; 19 | 20 | /** MediaType */ 21 | public static final MediaType JSON = MediaType.parse(APPLICATION_JSON); 22 | public static final MediaType OCTET = MediaType.parse(APPLICATION_OCTET_STREAM); 23 | public static final MediaType FORM = MediaType.parse(APPLICATION_FORM); 24 | public static final MediaType DEFAULT = JSON; 25 | 26 | /** 27 | * 判断两个 MediaType 是否相等,只判断 type 和 subType。 28 | */ 29 | public static boolean equals(@NonNull MediaType first, @NonNull MediaType second){ 30 | String first_type_subType = first.type().concat(first.subtype()); 31 | String second_type_subType = second.type().concat(second.subtype()); 32 | 33 | return TextUtils.equals(first_type_subType, second_type_subType); 34 | } 35 | 36 | public static boolean isJSON(@NonNull MediaType mediaType){ 37 | return equals(mediaType, JSON); 38 | } 39 | 40 | public static boolean isOCTET(@NonNull MediaType mediaType){ 41 | return equals(mediaType, OCTET); 42 | } 43 | 44 | public static boolean isFORM(@NonNull MediaType mediaType){ 45 | return equals(mediaType, FORM); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /mhttp/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wangchao/Work/android-sdk/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -dontwarn im.wangchao.** 19 | -dontwarn okio.** 20 | -dontwarn javax.annotation.Nullable 21 | -dontwarn javax.annotation.ParametersAreNonnullByDefault 22 | -keep class im.wangchao.** { *; } 23 | -keep class **_HttpBinder { *; } 24 | -keepclasseswithmembernames class * { 25 | @im.wangchao.*Description : Get.
14 | * 15 | *Author : wangchao.
16 | *Date : 16/6/6.
17 | *Time : 上午8:38.
18 | */ 19 | public class GetExample { 20 | 21 | public static Request doNormalRequest(){ 22 | return Request.builder().url("https://www.baidu.com") 23 | .callback(new TextCallbackHandler(){ 24 | @Override public void onSuccess(String data, Response response) { 25 | Log.e(MainActivity.TAG, "normal : " + data); 26 | } 27 | 28 | @Override public void onFailure(Response response, Throwable throwable) { 29 | Log.e(MainActivity.TAG, "onFailure : " + response.message() + " " + response.code()); 30 | } 31 | 32 | @Override public void onCancel() { 33 | Log.e(MainActivity.TAG, "onCancel"); 34 | } 35 | }) 36 | .build() 37 | .enqueue(); 38 | } 39 | 40 | public static void doAnnotationRequest(){ 41 | GetBaidu baidu = MHttp.create(GetBaidu.class); 42 | baidu.baidu(new TextCallbackHandler(){ 43 | @Override public void onSuccess(String data, Response response) { 44 | Log.e(MainActivity.TAG, data); 45 | } 46 | }); 47 | } 48 | 49 | public interface GetBaidu{ 50 | @Get(url = "https://www.baidu.com") 51 | void baidu(@Callback TextCallbackHandler callback); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/im/wangchao/mhttpdemo/PostExample.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttpdemo; 2 | 3 | import im.wangchao.http.annotations.Callback; 4 | import im.wangchao.http.annotations.Post; 5 | import im.wangchao.mhttp.callback.JSONCallbackHandler; 6 | import im.wangchao.mhttp.MHttp; 7 | import im.wangchao.mhttp.Request; 8 | import im.wangchao.mhttp.Response; 9 | import im.wangchao.mhttp.callback.TextCallbackHandler; 10 | 11 | /** 12 | *Description : PostExample.
13 | * 14 | *Author : wangchao.
15 | *Date : 16/6/6.
16 | *Time : 上午8:49.
17 | */ 18 | public class PostExample { 19 | 20 | public static void doNormalPost(){ 21 | Request.builder().url("http://wangchao.im") 22 | .addHeader("key", "value") 23 | .addParameter("key", "value") 24 | .callback(new JSONCallbackHandler(){ 25 | @Override public void onSuccess(JSON data, Response response) { 26 | 27 | } 28 | }) 29 | .build() 30 | .enqueue(); 31 | } 32 | 33 | public static void executeAnnotationPost(){ 34 | PostApi api = MHttp.create(PostApi.class); 35 | api.autoExecuteRequest("aa", "bb", new TextCallbackHandler(){ 36 | @Override public void onSuccess(String data, Response response) { 37 | //Todo 38 | } 39 | }); 40 | } 41 | 42 | public static Request getRequest(){ 43 | PostApi api = MHttp.create(PostApi.class); 44 | return api.postRequest("aa", "bb", new TextCallbackHandler(){ 45 | @Override public void onSuccess(String data, Response response) { 46 | //Todo 47 | } 48 | }); 49 | } 50 | 51 | public interface PostApi{ 52 | 53 | @Post(url = "http://wangchao.im") 54 | Request postRequest(String param0, String param1, @Callback TextCallbackHandler callback); 55 | 56 | @Post(url = "http://wangchao.im") 57 | void autoExecuteRequest(String param0, String param1, @Callback TextCallbackHandler callback); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/im/wangchao/mhttpdemo/Cache.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttpdemo; 2 | 3 | import android.content.Context; 4 | import android.util.Log; 5 | 6 | import im.wangchao.mhttp.MHttp; 7 | import okhttp3.OkHttpClient; 8 | import okhttp3.Request; 9 | import okhttp3.Response; 10 | 11 | /** 12 | *Description : Cache.
13 | *Author : wangchao.
14 | *Date : 2017/6/23.
15 | *Time : 下午1:43.
16 | */ 17 | public class Cache { 18 | final static String TAG = "wcwcwc"; 19 | 20 | public static void setCacheDir(Context context){ 21 | OkHttpClient.Builder builder = new OkHttpClient.Builder(); 22 | 23 | OkHttpClient client = builder.addNetworkInterceptor(chain -> { 24 | log("addNetworkInterceptor!"); 25 | Request request = chain.request(); 26 | long t1 = System.nanoTime(); 27 | log(String.format("Sending request %s on %s%n%s", 28 | request.url(), chain.connection(), request.headers())); 29 | 30 | Response response = chain.proceed(request); 31 | 32 | long t2 = System.nanoTime(); 33 | log(String.format("Received response for %s in %.1fms%n%s", 34 | response.request().url(), (t2 - t1) / 1e6d, response.headers())); 35 | 36 | return response.newBuilder() 37 | .header("Cache-Control", "max-age=60") 38 | .removeHeader("Pragma") 39 | .build(); 40 | }).addInterceptor(chain -> { 41 | Request request = chain.request(); 42 | 43 | log("addInterceptor!"); 44 | Response response = chain.proceed(request); 45 | 46 | return response.newBuilder() 47 | .header("Cache-Control", "max-age=60") 48 | .removeHeader("Pragma") 49 | .build(); 50 | }).build(); 51 | 52 | //Set Cache Dir 53 | MHttp.instance() 54 | .customOkHttpClient(client) 55 | .cache(context, "tempcache"); 56 | } 57 | 58 | private static void log(String msg){ 59 | Log.e(TAG, msg); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion rootProject.ext.compileSdkVersion 5 | 6 | defaultConfig { 7 | applicationId "im.wangchao.mhttp" 8 | minSdkVersion rootProject.ext.minSdkVersion 9 | targetSdkVersion rootProject.ext.targetSdkVersion 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | javaCompileOptions { 16 | annotationProcessorOptions { 17 | includeCompileClasspath = true 18 | } 19 | } 20 | } 21 | signingConfigs { 22 | release { 23 | storeFile file("./test") 24 | storePassword "123456" 25 | keyAlias "alias" 26 | keyPassword "123456" 27 | } 28 | } 29 | buildTypes { 30 | release { 31 | minifyEnabled true 32 | signingConfig signingConfigs.release 33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | packagingOptions { 37 | exclude 'META-INF/services/javax.annotation.processing.Processor' 38 | } 39 | compileOptions { 40 | sourceCompatibility rootProject.ext.sourceCompatibilityVersion 41 | targetCompatibility rootProject.ext.targetCompatibilityVersion 42 | } 43 | lintOptions { 44 | abortOnError false 45 | } 46 | 47 | } 48 | 49 | dependencies { 50 | implementation fileTree(include: ['*.jar'], dir: 'libs') 51 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { 52 | exclude group: 'com.android.support', module: 'support-annotations' 53 | }) 54 | testImplementation 'junit:junit:4.12' 55 | implementation 'com.android.support:appcompat-v7:25.3.1' 56 | implementation 'com.android.support:design:25.3.1' 57 | 58 | implementation deps.rxandroid 59 | implementation deps.rxjava 60 | // annotationProcessor 'im.wangchao:mhttp-compiler:0.5.1' 61 | implementation 'im.wangchao:mhttp:1.10.0' 62 | // implementation project(':mhttp') 63 | // annotationProcessor project(':mhttp-compiler') 64 | } 65 | 66 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/body/ProgressRequestBody.java: -------------------------------------------------------------------------------- 1 | package im.wangchao.mhttp.body; 2 | 3 | import android.support.annotation.NonNull; 4 | 5 | import java.io.IOException; 6 | 7 | import im.wangchao.mhttp.AbsCallbackHandler; 8 | import okhttp3.MediaType; 9 | import okhttp3.RequestBody; 10 | import okio.Buffer; 11 | import okio.BufferedSink; 12 | import okio.ForwardingSink; 13 | import okio.Okio; 14 | import okio.Sink; 15 | 16 | /** 17 | *Description : ProgressRequestBody.
18 | *Author : wangchao.
19 | *Date : 2018/1/30.
20 | *Time : 下午8:25.
21 | */ 22 | public class ProgressRequestBody extends RequestBody { 23 | 24 | private final RequestBody mRequestBody; 25 | private final AbsCallbackHandler mCallback; 26 | private BufferedSink mBufferedSink; 27 | 28 | public ProgressRequestBody(RequestBody requestBody, AbsCallbackHandler callback){ 29 | this.mRequestBody = requestBody; 30 | this.mCallback = callback; 31 | } 32 | 33 | @Override public MediaType contentType() { 34 | return mRequestBody.contentType(); 35 | } 36 | 37 | @Override public long contentLength() throws IOException { 38 | return mRequestBody.contentLength(); 39 | } 40 | 41 | @Override public void writeTo(@NonNull BufferedSink sink) throws IOException { 42 | if (mCallback == null){ 43 | mRequestBody.writeTo(sink); 44 | return; 45 | } 46 | if (mBufferedSink == null){ 47 | mBufferedSink = Okio.buffer(forward(sink)); 48 | } 49 | 50 | mRequestBody.writeTo(mBufferedSink); 51 | mBufferedSink.flush(); 52 | } 53 | 54 | private Sink forward(Sink sink){ 55 | return new ForwardingSink(sink) { 56 | private long bytesWritten = 0L; 57 | private long contentLength = 0L; 58 | 59 | @Override public void write(@NonNull Buffer source, long byteCount) throws IOException { 60 | super.write(source, byteCount); 61 | if (contentLength == 0) { 62 | contentLength = contentLength(); 63 | } 64 | bytesWritten += byteCount; 65 | 66 | mCallback.sendUploadProgressEvent((int) bytesWritten, (int) contentLength); 67 | } 68 | }; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /mhttp/src/main/java/im/wangchao/mhttp/internal/cookie/cache/SetCookieCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Francisco José Montiel Navarro. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package im.wangchao.mhttp.internal.cookie.cache; 18 | 19 | import java.util.Collection; 20 | import java.util.HashSet; 21 | import java.util.Iterator; 22 | import java.util.Set; 23 | 24 | import okhttp3.Cookie; 25 | 26 | public class SetCookieCache implements CookieCache { 27 | 28 | private SetDescription : ResponseExecuteObservable.
15 | *Author : wangchao.
16 | *Date : 2018/3/19.
17 | *Time : 下午4:46.
18 | */ 19 | public class ResponseExecuteObservable
28 | *
29 | * This new behaviour will be useful in determining when an already existing cookie in session must be overwritten.
30 | */
31 | class IdentifiableCookie {
32 |
33 | private Cookie cookie;
34 |
35 | static List Description : OctetStreamBody. Author : wangchao. Date : 16/3/8. Time : 下午4:31. Description : FileResponseHandler. Author : wangchao. Date : 15/10/18. Time : 下午2:39. Description : BindingClass. Author : wangchao. Date : 15/10/18. Time : 上午7:56. Description : ResponseEnqueueObservable. Author : wangchao. Date : 2018/3/19. Time : 下午4:45. Description : RxRequest. Author : wangchao. Date : 2018/3/26. Time : 下午5:40. Description : Android5SSL. Author : wangchao. Date : 2018/3/19. Time : 上午11:09. Description : HTTPS. Author : wangchao. Date : 16/9/2. Time : 下午3:58. Description : MHttp. Author : wangchao. Date : 16/6/2. Time : 上午8:40. Description : Android5SSL. Author : wangchao. Date : 2017/6/23. Time : 下午1:41. Description : InjectMethod. Author : wangchao. Date : 15/10/18. Time : 下午7:25. Description : AbsResponseHandler.
23 | * Callback lifecycle as follow:
24 | * onStart()
25 | * -------------------------------------------------------
26 | * |
27 | * |
28 | * is canceled --- Y --- onCancel()
29 | * |
30 | * N
31 | * |
32 | * onFinish()
33 | * |
34 | * |
35 | * is successful --- N --- onFailure() ------------------
36 | * | |
37 | * Y |
38 | * | |
39 | * backgroundParser() --is download-- onProgress() |
40 | * | | |
41 | * | | |
42 | * onSuccess() onSuccess() |
43 | * | | |
44 | * | | |
45 | * ---------------------------------------------------------
46 | * onFinally()
47 | * Author : wangchao. Date : 15/8/17. Time : 下午5:56.