├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── build │ └── generated │ │ └── source │ │ └── kapt │ │ └── debug │ │ └── rxhttp │ │ └── wrapper │ │ └── param │ │ ├── BaseRxHttp.java │ │ ├── ObservableCall.java │ │ ├── ObservableCallEnqueue.java │ │ ├── ObservableCallExecute.java │ │ ├── ObservableParser.java │ │ ├── RxHttp.java │ │ ├── RxHttp.kt │ │ ├── RxHttpBodyParam.java │ │ ├── RxHttpFormParam.java │ │ ├── RxHttpJsonArrayParam.java │ │ ├── RxHttpJsonParam.java │ │ └── RxHttpNoBodyParam.java ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── example │ │ └── coroutine │ │ ├── MainActivity.kt │ │ └── Url.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.xml │ ├── mipmap-anydpi-v26 │ ├── ic_launcher.xml │ └── ic_launcher_round.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml │ └── xml │ └── network_config.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rxlife-coroutine ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ ├── androidx │ └── lifecycle │ │ └── RxLife.kt │ └── com │ └── rxlife │ └── coroutine │ └── RxLifeScope.kt └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![](https://img.shields.io/badge/QQ群-378530627-red.svg)](https://jq.qq.com/?_wv=1027&k=E53Hakvv) 2 | 3 | # 该项目已停止维护,正式被抛弃,原因如下: 4 | 5 | 1、同一个`FragmentActivity/Fragment`下,`rxLifeScope`与`lifecycleScope`不能共用;同一个ViewModel下,`rxLifeScope`与`viewModelScope`不能共用 6 | 7 | 2、配合[RxHttp](https://github.com/liujingxing/RxHttp)发请求时,每次都要开启一个协程来捕获异常,这对于再次封装的人,非常不友好; 8 | 目前`RxHttp`的`awaitResult`操作符一样可以捕获异常,所以`rxLifeScope`就没了用武之地,是时候退出历史舞台了 9 | 10 | 3、不能同`lifecycleScope`或`viewModelScope`一样,开启协程时,传入`CoroutineContext`或`CoroutineStart`参数 11 | 亦没有一系列`launchXxx`方法 12 | 13 | 4、`rxLifeScope`配合`RxHttp v2.6.6`及以上版本发请求时,调用`async`方法将导致请求结束回调不被调用 14 | 15 | 16 | # 替代品 17 | 18 | ```java 19 | //lifecycleScope 20 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1" 21 | //viewModelScope 22 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" 23 | ``` 24 | **使用** 25 | ```java 26 | //使用lifecycleScope或viewModelScope开协程,更多用法,请自行查阅相关资料 27 | lifecycleScope.launch { 28 | //请求开始 29 | RxHttp.get("...") 30 | .toStr() 31 | .awaitResult { 32 | //请求成功 33 | }.onFailure { 34 | //请求异常 35 | } 36 | //请求结束 37 | } 38 | ``` 39 | 其中`awaitResult`只是`RxHttp`的一种异常处理操作符,更多异常操作符,请查看[RxHttp ,比Retrofit 更优雅的协程体验](https://juejin.cn/post/6844904100090347528) 40 | 41 | 以上代码,同样可以做到页面销毁,协程自动关闭,协程关闭,请求跟着关闭 42 | 43 | 44 | 45 | # 更新日志 46 | 47 | ### v2.2.0 (2021-09-01) 48 | 49 | - rxLifeScope标记为过时,正式退出历史舞台 50 | 51 | ### v2.0.1 (2020-09-14) 52 | 53 | - 修改:协程被关闭后,若有异常,不再走失败回调 54 | 55 | - 修复:捕获失败回调里的异常,并打印日志 56 | 57 | 58 | 59 | 60 | # 简介 61 | 62 | ***RxLife-Coroutine 优势*** 63 | 64 | - ***开启协程,并自动管理协程生命周期,在页面销毁时,自动关闭协程*** 65 | 66 | - ***自动捕获协程异常,通过回调可拿到异常信息*** 67 | 68 | - ***可监听协程开启及结束*** 69 | 70 | **gradle依赖** 71 | ```java 72 | dependencies { 73 | //管理协程生命周期,页面销毁,自动关闭协程 74 | implementation 'com.github.liujingxing.rxlife:rxlife-coroutine:2.2.0' 75 | } 76 | ``` 77 | 78 | 79 | # 使用 80 | ## FragmentActivity/Fragment/ViewModel环境下 81 | ```java 82 | rxLifeScope.launch({ 83 | //协程体 84 | }, { 85 | //异常回调 86 | }, { 87 | //协程开始回调 88 | }, { 89 | //协程结束回调,不管成功/失败都会回调 90 | }) 91 | ``` 92 | 93 | 以上代码,有两点需要注意 94 | 95 | - launch方法有共有4个参数,仅有第一个参数是必须的,其它3个参数都有默认值,可以不传 96 | 97 | - 我们无需手动关闭协程,在页面销毁时,会自动关闭协程,以防止内存泄露 98 | 99 | ## 非FragmentActivity/Fragment/ViewModel环境下 100 | ```java 101 | val job = RxLifeScope().launch({ 102 | //协程体 103 | }, { 104 | //异常回调 105 | }, { 106 | //协程开始回调 107 | }, { 108 | //协程结束回调,不管成功/失败都会回调 109 | }) 110 | 111 | //在合适的时机,手动关闭协程 112 | job.cancel() 113 | ``` 114 | 以上代码使用`RxLifeScope()`手动创建了RxLifeScope对象,这种方式,我们需要在合适的时机,拿到`Job`对象,关闭协程 115 | 116 | 117 | 118 | 119 | # Licenses 120 | ``` 121 | Copyright 2020 liujingxing 122 | 123 | Licensed under the Apache License, Version 2.0 (the "License"); 124 | you may not use this file except in compliance with the License. 125 | You may obtain a copy of the License at 126 | 127 | http://www.apache.org/licenses/LICENSE-2.0 128 | 129 | Unless required by applicable law or agreed to in writing, software 130 | distributed under the License is distributed on an "AS IS" BASIS, 131 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 132 | See the License for the specific language governing permissions and 133 | limitations under the License. 134 | ``` 135 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | id 'kotlin-kapt' 5 | } 6 | 7 | android { 8 | compileSdkVersion 29 9 | 10 | defaultConfig { 11 | applicationId "com.example.coroutine" 12 | minSdkVersion 15 13 | targetSdkVersion 29 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | javaCompileOptions { 18 | annotationProcessorOptions { 19 | arguments = [ 20 | rxhttp_rxjava: 'rxjava3' 21 | ] 22 | } 23 | } 24 | 25 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 26 | } 27 | 28 | buildTypes { 29 | release { 30 | minifyEnabled false 31 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 32 | } 33 | } 34 | 35 | compileOptions { 36 | sourceCompatibility JavaVersion.VERSION_1_8 37 | targetCompatibility JavaVersion.VERSION_1_8 38 | } 39 | 40 | kotlinOptions { 41 | jvmTarget = '1.8' 42 | } 43 | 44 | buildFeatures { 45 | dataBinding = true 46 | } 47 | } 48 | 49 | dependencies { 50 | implementation fileTree(dir: 'libs', include: ['*.jar']) 51 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 52 | implementation 'androidx.appcompat:appcompat:1.2.0' 53 | implementation 'androidx.core:core-ktx:1.3.2' 54 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 55 | 56 | // implementation project(":rxlife-coroutine") 57 | 58 | implementation 'com.ljx.rxlife:rxlife-coroutine:2.0.1' 59 | 60 | implementation "com.squareup.okhttp3:okhttp:${okhttp_version}" 61 | 62 | implementation "com.github.liujingxing.rxhttp:rxhttp:${rxhttp_version}" 63 | kapt "com.github.liujingxing.rxhttp:rxhttp-compiler:${rxhttp_version}" 64 | implementation "androidx.fragment:fragment-ktx:1.2.5" 65 | 66 | implementation 'io.reactivex.rxjava3:rxjava:3.0.6' 67 | implementation 'io.reactivex.rxjava3:rxandroid:3.0.0' 68 | implementation 'com.ljx.rxlife3:rxlife-rxjava:3.0.0' 69 | 70 | implementation "androidx.lifecycle:lifecycle-service:2.2.0" 71 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0" 72 | implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0" 73 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" 74 | 75 | testImplementation 'junit:junit:4.13.2' 76 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 77 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 78 | } 79 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/BaseRxHttp.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import android.content.Context; 4 | import android.graphics.Bitmap; 5 | import android.net.Uri; 6 | 7 | import java.io.File; 8 | import java.lang.reflect.Type; 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | import io.reactivex.rxjava3.core.Observable; 13 | import io.reactivex.rxjava3.core.Scheduler; 14 | import io.reactivex.rxjava3.functions.Consumer; 15 | import io.reactivex.rxjava3.plugins.RxJavaPlugins; 16 | import io.reactivex.rxjava3.schedulers.Schedulers; 17 | import okhttp3.Headers; 18 | import okhttp3.Response; 19 | import rxhttp.IRxHttp; 20 | import rxhttp.wrapper.OkHttpCompat; 21 | import rxhttp.wrapper.callback.OutputStreamFactory; 22 | import rxhttp.wrapper.callback.UriFactory; 23 | import rxhttp.wrapper.entity.ParameterizedTypeImpl; 24 | import rxhttp.wrapper.entity.Progress; 25 | import rxhttp.wrapper.parse.BitmapParser; 26 | import rxhttp.wrapper.parse.OkResponseParser; 27 | import rxhttp.wrapper.parse.Parser; 28 | import rxhttp.wrapper.parse.SimpleParser; 29 | import rxhttp.wrapper.parse.StreamParser; 30 | import rxhttp.wrapper.utils.LogUtil; 31 | import rxhttp.wrapper.utils.UriUtil; 32 | 33 | /** 34 | * 本类存放asXxx方法(需要单独依赖RxJava,并告知RxHttp依赖的RxJava版本) 35 | * 如未生成,请查看 https://github.com/liujingxing/okhttp-RxHttp/wiki/FAQ 36 | * User: ljx 37 | * Date: 2020/4/11 38 | * Time: 18:15 39 | */ 40 | public abstract class BaseRxHttp implements IRxHttp { 41 | 42 | static { 43 | Consumer errorHandler = RxJavaPlugins.getErrorHandler(); 44 | if (errorHandler == null) { 45 | /* 46 | RxJava2的一个重要的设计理念是:不吃掉任何一个异常, 即抛出的异常无人处理,便会导致程序崩溃 47 | 这就会导致一个问题,当RxJava2“downStream”取消订阅后,“upStream”仍有可能抛出异常, 48 | 这时由于已经取消订阅,“downStream”无法处理异常,此时的异常无人处理,便会导致程序崩溃 49 | */ 50 | RxJavaPlugins.setErrorHandler(LogUtil::log); 51 | } 52 | } 53 | 54 | public abstract Observable asParser(Parser parser, Scheduler scheduler, Consumer progressConsumer); 55 | 56 | public Observable asParser(Parser parser) { 57 | return asParser(parser, null, null); 58 | } 59 | 60 | public final Observable asClass(Class type) { 61 | return asParser(new SimpleParser<>(type)); 62 | } 63 | 64 | public final Observable asString() { 65 | return asClass(String.class); 66 | } 67 | 68 | public final Observable asBoolean() { 69 | return asClass(Boolean.class); 70 | } 71 | 72 | public final Observable asByte() { 73 | return asClass(Byte.class); 74 | } 75 | 76 | public final Observable asShort() { 77 | return asClass(Short.class); 78 | } 79 | 80 | public final Observable asInteger() { 81 | return asClass(Integer.class); 82 | } 83 | 84 | public final Observable asLong() { 85 | return asClass(Long.class); 86 | } 87 | 88 | public final Observable asFloat() { 89 | return asClass(Float.class); 90 | } 91 | 92 | public final Observable asDouble() { 93 | return asClass(Double.class); 94 | } 95 | 96 | public final Observable> asMap(Class kType) { 97 | return asMap(kType, kType); 98 | } 99 | 100 | public final Observable> asMap(Class kType, Class vType) { 101 | Type tTypeMap = ParameterizedTypeImpl.getParameterized(Map.class, kType, vType); 102 | return asParser(new SimpleParser>(tTypeMap)); 103 | } 104 | 105 | public final Observable> asList(Class tType) { 106 | Type tTypeList = ParameterizedTypeImpl.get(List.class, tType); 107 | return asParser(new SimpleParser>(tTypeList)); 108 | } 109 | 110 | public final Observable asBitmap() { 111 | return asParser(new BitmapParser()); 112 | } 113 | 114 | public final Observable asOkResponse() { 115 | return asParser(new OkResponseParser()); 116 | } 117 | 118 | public final Observable asHeaders() { 119 | return asOkResponse() 120 | .map(response -> { 121 | try { 122 | return response.headers(); 123 | } finally { 124 | OkHttpCompat.closeQuietly(response); 125 | } 126 | }); 127 | } 128 | 129 | public final Observable asDownload(String destPath) { 130 | return asDownload(destPath, null, null); 131 | } 132 | 133 | public final Observable asDownload(String destPath, 134 | Consumer progressConsumer) { 135 | return asDownload(destPath, null, progressConsumer); 136 | } 137 | 138 | public final Observable asDownload(String destPath, Scheduler scheduler, 139 | Consumer progressConsumer) { 140 | return asParser(StreamParser.get(destPath), scheduler, progressConsumer); 141 | } 142 | 143 | public final Observable asDownload(Context context, Uri uri) { 144 | return asDownload(context, uri, null, null); 145 | } 146 | 147 | public final Observable asDownload(Context context, Uri uri, Scheduler scheduler, 148 | Consumer progressConsumer) { 149 | return asParser(StreamParser.get(context, uri), scheduler, progressConsumer); 150 | } 151 | 152 | public final Observable asDownload(OutputStreamFactory osFactory) { 153 | return asDownload(osFactory, null, null); 154 | } 155 | 156 | public final Observable asDownload(OutputStreamFactory osFactory, Scheduler scheduler, 157 | Consumer progressConsumer) { 158 | return asParser(new StreamParser(osFactory), scheduler, progressConsumer); 159 | } 160 | 161 | public final Observable asAppendDownload(String destPath) { 162 | return asAppendDownload(destPath, null, null); 163 | } 164 | 165 | public final Observable asAppendDownload(String destPath, Scheduler scheduler, 166 | Consumer progressConsumer) { 167 | long fileLength = new File(destPath).length(); 168 | setRangeHeader(fileLength, -1, true); 169 | return asParser(StreamParser.get(destPath), scheduler, progressConsumer); 170 | } 171 | 172 | public final Observable asAppendDownload(Context context, Uri uri) { 173 | return asAppendDownload(context, uri, null, null); 174 | } 175 | 176 | public final Observable asAppendDownload(Context context, Uri uri, Scheduler scheduler, 177 | Consumer progressConsumer) { 178 | return Observable 179 | .fromCallable(() -> { 180 | long length = UriUtil.length(uri, context); 181 | if (length >= 0) setRangeHeader(length, -1, true); 182 | return StreamParser.get(context, uri); 183 | }) 184 | .subscribeOn(Schedulers.io()) 185 | .flatMap(parser -> { 186 | return asParser(parser, scheduler, progressConsumer); 187 | }); 188 | } 189 | 190 | public final Observable asAppendDownload(UriFactory uriFactory) { 191 | return asAppendDownload(uriFactory, null, null); 192 | } 193 | 194 | public final Observable asAppendDownload(UriFactory uriFactory, Scheduler scheduler, 195 | Consumer progressConsumer) { 196 | return Observable 197 | .fromCallable(() -> { 198 | Uri uri = uriFactory.query(); 199 | StreamParser parser; 200 | if (uri != null) { 201 | long length = UriUtil.length(uri, uriFactory.getContext()); 202 | if (length >= 0) 203 | setRangeHeader(length, -1, true); 204 | parser = StreamParser.get(uriFactory.getContext(), uri); 205 | } else { 206 | parser = new StreamParser(uriFactory); 207 | } 208 | return parser; 209 | }) 210 | .subscribeOn(Schedulers.io()) 211 | .flatMap(parser -> { 212 | return asParser(parser, scheduler, progressConsumer); 213 | }); 214 | } 215 | 216 | } 217 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/ObservableCall.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | import io.reactivex.rxjava3.core.Scheduler; 5 | import io.reactivex.rxjava3.functions.Consumer; 6 | import rxhttp.wrapper.entity.Progress; 7 | import rxhttp.wrapper.parse.Parser; 8 | 9 | /** 10 | * User: ljx 11 | * Date: 2020/9/5 12 | * Time: 21:59 13 | */ 14 | abstract class ObservableCall extends Observable { 15 | 16 | public Observable asParser(Parser parser) { 17 | return asParser(parser, null, null); 18 | } 19 | 20 | public Observable asParser(Parser parser, Consumer progressConsumer) { 21 | return asParser(parser, null, progressConsumer); 22 | } 23 | 24 | public Observable asParser(Parser parser, Scheduler scheduler, Consumer progressConsumer) { 25 | return new ObservableParser<>(this, parser, scheduler, progressConsumer); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/ObservableCallEnqueue.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import java.io.IOException; 4 | 5 | import io.reactivex.rxjava3.core.Observer; 6 | import io.reactivex.rxjava3.disposables.Disposable; 7 | import io.reactivex.rxjava3.exceptions.Exceptions; 8 | import io.reactivex.rxjava3.plugins.RxJavaPlugins; 9 | import okhttp3.Call; 10 | import okhttp3.Callback; 11 | import okhttp3.Response; 12 | import rxhttp.IRxHttp; 13 | import rxhttp.wrapper.callback.ProgressCallback; 14 | import rxhttp.wrapper.entity.Progress; 15 | import rxhttp.wrapper.entity.ProgressT; 16 | import rxhttp.wrapper.utils.LogUtil; 17 | 18 | /** 19 | * User: ljx 20 | * Date: 2018/04/20 21 | * Time: 11:15 22 | */ 23 | final class ObservableCallEnqueue extends ObservableCall { 24 | 25 | private IRxHttp iRxHttp; 26 | private boolean callbackUploadProgress; 27 | 28 | ObservableCallEnqueue(IRxHttp iRxHttp) { 29 | this(iRxHttp, false); 30 | } 31 | 32 | ObservableCallEnqueue(IRxHttp iRxHttp, boolean callbackUploadProgress) { 33 | this.iRxHttp = iRxHttp; 34 | this.callbackUploadProgress = callbackUploadProgress; 35 | } 36 | 37 | @Override 38 | public void subscribeActual(Observer observer) { 39 | HttpDisposable d = new HttpDisposable(observer, iRxHttp, callbackUploadProgress); 40 | observer.onSubscribe(d); 41 | if (d.isDisposed()) { 42 | return; 43 | } 44 | d.run(); 45 | } 46 | 47 | 48 | private static class HttpDisposable implements Disposable, Callback, ProgressCallback { 49 | 50 | private volatile boolean disposed; 51 | 52 | private final Call call; 53 | private final Observer downstream; 54 | 55 | /** 56 | * Constructs a DeferredScalarDisposable by wrapping the Observer. 57 | * 58 | * @param downstream the Observer to wrap, not null (not verified) 59 | */ 60 | HttpDisposable(Observer downstream, IRxHttp iRxHttp, boolean callbackUploadProgress) { 61 | if (iRxHttp instanceof RxHttpAbstractBodyParam && callbackUploadProgress) { 62 | RxHttpAbstractBodyParam bodyParam = (RxHttpAbstractBodyParam) iRxHttp; 63 | bodyParam.getParam().setProgressCallback(this); 64 | } 65 | this.downstream = downstream; 66 | this.call = iRxHttp.newCall(); 67 | } 68 | 69 | @Override 70 | public void onProgress(Progress p) { 71 | if (!disposed) { 72 | downstream.onNext(p); 73 | } 74 | } 75 | 76 | @Override 77 | public void onResponse(Call call, Response response) throws IOException { 78 | if (!disposed) { 79 | downstream.onNext(new ProgressT<>(response)); 80 | } 81 | if (!disposed) { 82 | downstream.onComplete(); 83 | } 84 | } 85 | 86 | @Override 87 | public void onFailure(Call call, IOException e) { 88 | LogUtil.log(call.request().url().toString(), e); 89 | Exceptions.throwIfFatal(e); 90 | if (!disposed) { 91 | downstream.onError(e); 92 | } else { 93 | RxJavaPlugins.onError(e); 94 | } 95 | } 96 | 97 | @Override 98 | public void dispose() { 99 | disposed = true; 100 | call.cancel(); 101 | } 102 | 103 | @Override 104 | public boolean isDisposed() { 105 | return disposed; 106 | } 107 | 108 | public void run() { 109 | call.enqueue(this); 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/ObservableCallExecute.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import io.reactivex.rxjava3.core.Observer; 4 | import io.reactivex.rxjava3.disposables.Disposable; 5 | import io.reactivex.rxjava3.exceptions.Exceptions; 6 | import io.reactivex.rxjava3.plugins.RxJavaPlugins; 7 | import okhttp3.Call; 8 | import okhttp3.Response; 9 | import rxhttp.IRxHttp; 10 | import rxhttp.wrapper.callback.ProgressCallback; 11 | import rxhttp.wrapper.entity.Progress; 12 | import rxhttp.wrapper.entity.ProgressT; 13 | import rxhttp.wrapper.utils.LogUtil; 14 | 15 | /** 16 | * User: ljx 17 | * Date: 2018/04/20 18 | * Time: 11:15 19 | */ 20 | final class ObservableCallExecute extends ObservableCall { 21 | 22 | private IRxHttp iRxHttp; 23 | private boolean callbackUploadProgress; 24 | 25 | ObservableCallExecute(IRxHttp iRxHttp) { 26 | this(iRxHttp, false); 27 | } 28 | 29 | ObservableCallExecute(IRxHttp iRxHttp, boolean callbackUploadProgress) { 30 | this.iRxHttp = iRxHttp; 31 | this.callbackUploadProgress = callbackUploadProgress; 32 | } 33 | 34 | @Override 35 | public void subscribeActual(Observer observer) { 36 | HttpDisposable d = new HttpDisposable(observer, iRxHttp, callbackUploadProgress); 37 | observer.onSubscribe(d); 38 | if (d.isDisposed()) { 39 | return; 40 | } 41 | d.run(); 42 | } 43 | 44 | private static class HttpDisposable implements Disposable, ProgressCallback { 45 | 46 | private boolean fusionMode; 47 | private volatile boolean disposed; 48 | 49 | private final Call call; 50 | private final Observer downstream; 51 | 52 | /** 53 | * Constructs a DeferredScalarDisposable by wrapping the Observer. 54 | * 55 | * @param downstream the Observer to wrap, not null (not verified) 56 | */ 57 | HttpDisposable(Observer downstream, IRxHttp iRxHttp, boolean callbackUploadProgress) { 58 | if (iRxHttp instanceof RxHttpAbstractBodyParam && callbackUploadProgress) { 59 | RxHttpAbstractBodyParam bodyParam = (RxHttpAbstractBodyParam) iRxHttp; 60 | bodyParam.getParam().setProgressCallback(this); 61 | } 62 | this.downstream = downstream; 63 | this.call = iRxHttp.newCall(); 64 | } 65 | 66 | @Override 67 | public void onProgress(Progress p) { 68 | if (!disposed) { 69 | downstream.onNext(p); 70 | } 71 | } 72 | 73 | public void run() { 74 | Response value; 75 | try { 76 | value = call.execute(); 77 | } catch (Throwable e) { 78 | LogUtil.log(call.request().url().toString(), e); 79 | Exceptions.throwIfFatal(e); 80 | if (!disposed) { 81 | downstream.onError(e); 82 | } else { 83 | RxJavaPlugins.onError(e); 84 | } 85 | return; 86 | } 87 | if (!disposed) { 88 | downstream.onNext(new ProgressT<>(value)); 89 | } 90 | if (!disposed) { 91 | downstream.onComplete(); 92 | } 93 | } 94 | 95 | @Override 96 | public void dispose() { 97 | disposed = true; 98 | call.cancel(); 99 | } 100 | 101 | @Override 102 | public boolean isDisposed() { 103 | return disposed; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/ObservableParser.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import java.util.Objects; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | import io.reactivex.rxjava3.core.Observable; 7 | import io.reactivex.rxjava3.core.ObservableSource; 8 | import io.reactivex.rxjava3.core.Observer; 9 | import io.reactivex.rxjava3.exceptions.Exceptions; 10 | import io.reactivex.rxjava3.internal.fuseable.SimplePlainQueue; 11 | import io.reactivex.rxjava3.internal.queue.SpscArrayQueue; 12 | import io.reactivex.rxjava3.plugins.RxJavaPlugins; 13 | import io.reactivex.rxjava3.core.Scheduler; 14 | import io.reactivex.rxjava3.disposables.Disposable; 15 | import io.reactivex.rxjava3.functions.Consumer; 16 | import io.reactivex.rxjava3.internal.disposables.DisposableHelper; 17 | import io.reactivex.rxjava3.core.Scheduler.Worker; 18 | 19 | import okhttp3.Response; 20 | import rxhttp.wrapper.annotations.NonNull; 21 | import rxhttp.wrapper.annotations.Nullable; 22 | import rxhttp.wrapper.callback.ProgressCallback; 23 | import rxhttp.wrapper.entity.Progress; 24 | import rxhttp.wrapper.entity.ProgressT; 25 | import rxhttp.wrapper.parse.StreamParser; 26 | import rxhttp.wrapper.parse.Parser; 27 | import rxhttp.wrapper.utils.LogUtil; 28 | 29 | final class ObservableParser extends Observable { 30 | 31 | private final Parser parser; 32 | private final ObservableSource source; 33 | private final Scheduler scheduler; 34 | private final Consumer progressConsumer; 35 | 36 | ObservableParser(@NonNull ObservableSource source, @NonNull Parser parser, 37 | @Nullable Scheduler scheduler, @Nullable Consumer progressConsumer) { 38 | this.source = source; 39 | this.parser = parser; 40 | this.scheduler = scheduler; 41 | this.progressConsumer = progressConsumer; 42 | } 43 | 44 | @Override 45 | protected void subscribeActual(@NonNull Observer observer) { 46 | if (scheduler == null) { 47 | source.subscribe(new SyncParserObserver<>(observer, parser, progressConsumer)); 48 | } else { 49 | Worker worker = scheduler.createWorker(); 50 | source.subscribe(new AsyncParserObserver<>(observer, worker, progressConsumer, parser)); 51 | } 52 | } 53 | 54 | private static final class SyncParserObserver implements Observer, Disposable, ProgressCallback { 55 | private final Parser parser; 56 | 57 | private Disposable upstream; 58 | private final Observer downstream; 59 | private final Consumer progressConsumer; 60 | private boolean done; 61 | 62 | SyncParserObserver(Observer actual, Parser parser, Consumer progressConsumer) { 63 | this.downstream = actual; 64 | this.parser = parser; 65 | this.progressConsumer = progressConsumer; 66 | 67 | if (progressConsumer != null && parser instanceof StreamParser) { 68 | ((StreamParser) parser).setProgressCallback(this); 69 | } 70 | } 71 | 72 | @Override 73 | public void onSubscribe(Disposable d) { 74 | if (DisposableHelper.validate(this.upstream, d)) { 75 | this.upstream = d; 76 | downstream.onSubscribe(this); 77 | } 78 | } 79 | 80 | //download progress callback 81 | @Override 82 | public void onProgress(Progress p) { 83 | if (done) { 84 | return; 85 | } 86 | try { 87 | progressConsumer.accept(p); 88 | } catch (Throwable t) { 89 | fail(t); 90 | } 91 | } 92 | 93 | @SuppressWarnings("unchecked") 94 | @Override 95 | public void onNext(Progress progress) { 96 | if (done) { 97 | return; 98 | } 99 | if (progress instanceof ProgressT) { 100 | ProgressT p = (ProgressT) progress; 101 | T v; 102 | try { 103 | v = Objects.requireNonNull(parser.onParse(p.getResult()), "The onParse function returned a null value."); 104 | } catch (Throwable t) { 105 | LogUtil.log(p.getResult().request().url().toString(), t); 106 | fail(t); 107 | return; 108 | } 109 | downstream.onNext(v); 110 | } else { 111 | try { 112 | progressConsumer.accept(progress); 113 | } catch (Throwable t) { 114 | fail(t); 115 | } 116 | } 117 | } 118 | 119 | @Override 120 | public void onError(Throwable t) { 121 | if (done) { 122 | RxJavaPlugins.onError(t); 123 | return; 124 | } 125 | done = true; 126 | downstream.onError(t); 127 | } 128 | 129 | @Override 130 | public void onComplete() { 131 | if (done) { 132 | return; 133 | } 134 | done = true; 135 | downstream.onComplete(); 136 | } 137 | 138 | @Override 139 | public void dispose() { 140 | upstream.dispose(); 141 | } 142 | 143 | @Override 144 | public boolean isDisposed() { 145 | return upstream.isDisposed(); 146 | } 147 | 148 | private void fail(Throwable t) { 149 | Exceptions.throwIfFatal(t); 150 | upstream.dispose(); 151 | onError(t); 152 | } 153 | } 154 | 155 | 156 | private static final class AsyncParserObserver extends AtomicInteger 157 | implements Observer, Disposable, ProgressCallback, Runnable { 158 | 159 | private final Parser parser; 160 | private final Observer downstream; 161 | 162 | private Disposable upstream; 163 | private Throwable error; 164 | 165 | private volatile boolean done; 166 | private volatile boolean disposed; 167 | private final SimplePlainQueue queue; 168 | private final Scheduler.Worker worker; 169 | 170 | private final Consumer progressConsumer; 171 | 172 | AsyncParserObserver(Observer actual, Scheduler.Worker worker, Consumer progressConsumer, Parser parser) { 173 | this.downstream = actual; 174 | this.parser = parser; 175 | this.worker = worker; 176 | this.progressConsumer = progressConsumer; 177 | queue = new SpscArrayQueue<>(2); 178 | 179 | if (progressConsumer != null && parser instanceof StreamParser) { 180 | ((StreamParser) parser).setProgressCallback(this); 181 | } 182 | } 183 | 184 | @Override 185 | public void onSubscribe(@NonNull Disposable d) { 186 | if (DisposableHelper.validate(this.upstream, d)) { 187 | this.upstream = d; 188 | downstream.onSubscribe(this); 189 | } 190 | } 191 | 192 | //download progress callback 193 | @Override 194 | public void onProgress(Progress p) { 195 | if (done) { 196 | return; 197 | } 198 | offer(p); 199 | } 200 | 201 | @SuppressWarnings("unchecked") 202 | @Override 203 | public void onNext(Progress progress) { 204 | if (done) { 205 | return; 206 | } 207 | ProgressT pt = null; 208 | if (progress instanceof ProgressT) { 209 | ProgressT progressT = (ProgressT) progress; 210 | try { 211 | T t = Objects.requireNonNull(parser.onParse(progressT.getResult()), "The onParse function returned a null value."); 212 | pt = new ProgressT<>(t); 213 | } catch (Throwable t) { 214 | LogUtil.log(progressT.getResult().request().url().toString(), t); 215 | onError(t); 216 | return; 217 | } 218 | } 219 | Progress p = pt != null ? pt : progress; 220 | offer(p); 221 | } 222 | 223 | private void offer(Progress p) { 224 | if (!queue.offer(p)) { 225 | queue.poll(); 226 | queue.offer(p); 227 | } 228 | schedule(); 229 | } 230 | 231 | @Override 232 | public void onError(Throwable t) { 233 | if (done) { 234 | RxJavaPlugins.onError(t); 235 | return; 236 | } 237 | error = t; 238 | done = true; 239 | schedule(); 240 | } 241 | 242 | @Override 243 | public void onComplete() { 244 | if (done) { 245 | return; 246 | } 247 | done = true; 248 | schedule(); 249 | } 250 | 251 | 252 | void schedule() { 253 | if (getAndIncrement() == 0) { 254 | worker.schedule(this); 255 | } 256 | } 257 | 258 | @SuppressWarnings("unchecked") 259 | @Override 260 | public void run() { 261 | int missed = 1; 262 | 263 | final SimplePlainQueue q = queue; 264 | final Observer a = downstream; 265 | while (!checkTerminated(done, q.isEmpty(), a)) { 266 | for (; ; ) { 267 | boolean d = done; 268 | Progress p; 269 | try { 270 | p = q.poll(); 271 | 272 | boolean empty = p == null; 273 | 274 | if (checkTerminated(d, empty, a)) { 275 | return; 276 | } 277 | if (empty) { 278 | break; 279 | } 280 | if (p instanceof ProgressT) { 281 | a.onNext(((ProgressT) p).getResult()); 282 | } else { 283 | progressConsumer.accept(p); 284 | } 285 | } catch (Throwable ex) { 286 | Exceptions.throwIfFatal(ex); 287 | disposed = true; 288 | upstream.dispose(); 289 | q.clear(); 290 | a.onError(ex); 291 | worker.dispose(); 292 | return; 293 | } 294 | } 295 | missed = addAndGet(-missed); 296 | if (missed == 0) { 297 | break; 298 | } 299 | } 300 | } 301 | 302 | boolean checkTerminated(boolean d, boolean empty, Observer a) { 303 | if (isDisposed()) { 304 | queue.clear(); 305 | return true; 306 | } 307 | if (d) { 308 | Throwable e = error; 309 | if (e != null) { 310 | disposed = true; 311 | queue.clear(); 312 | a.onError(e); 313 | worker.dispose(); 314 | return true; 315 | } else if (empty) { 316 | disposed = true; 317 | a.onComplete(); 318 | worker.dispose(); 319 | return true; 320 | } 321 | } 322 | return false; 323 | } 324 | 325 | @Override 326 | public void dispose() { 327 | if (!disposed) { 328 | disposed = true; 329 | upstream.dispose(); 330 | worker.dispose(); 331 | if (getAndIncrement() == 0) { 332 | queue.clear(); 333 | } 334 | } 335 | } 336 | 337 | @Override 338 | public boolean isDisposed() { 339 | return disposed; 340 | } 341 | } 342 | } 343 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttp.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import com.example.coroutine.Url; 4 | import io.reactivex.rxjava3.core.Observable; 5 | import io.reactivex.rxjava3.core.Scheduler; 6 | import io.reactivex.rxjava3.disposables.Disposable; 7 | import io.reactivex.rxjava3.functions.Consumer; 8 | import java.io.IOException; 9 | import java.lang.Class; 10 | import java.lang.Deprecated; 11 | import java.lang.Object; 12 | import java.lang.Override; 13 | import java.lang.String; 14 | import java.lang.SuppressWarnings; 15 | import java.lang.reflect.Type; 16 | import java.util.List; 17 | import java.util.Map; 18 | import java.util.concurrent.TimeUnit; 19 | import okhttp3.CacheControl; 20 | import okhttp3.Call; 21 | import okhttp3.Headers; 22 | import okhttp3.Headers.Builder; 23 | import okhttp3.OkHttpClient; 24 | import okhttp3.Request; 25 | import okhttp3.Response; 26 | import rxhttp.RxHttpPlugins; 27 | import rxhttp.wrapper.cahce.CacheMode; 28 | import rxhttp.wrapper.cahce.CacheStrategy; 29 | import rxhttp.wrapper.callback.IConverter; 30 | import rxhttp.wrapper.entity.DownloadOffSize; 31 | import rxhttp.wrapper.entity.ParameterizedTypeImpl; 32 | import rxhttp.wrapper.entity.Progress; 33 | import rxhttp.wrapper.intercept.CacheInterceptor; 34 | import rxhttp.wrapper.parse.Parser; 35 | import rxhttp.wrapper.parse.SimpleParser; 36 | import rxhttp.wrapper.utils.LogTime; 37 | import rxhttp.wrapper.utils.LogUtil; 38 | 39 | /** 40 | * Github 41 | * https://github.com/liujingxing/RxHttp 42 | * https://github.com/liujingxing/RxLife 43 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/FAQ 44 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/更新日志 45 | */ 46 | @SuppressWarnings("unchecked") 47 | public class RxHttp

extends BaseRxHttp { 48 | private int connectTimeoutMillis; 49 | 50 | private int readTimeoutMillis; 51 | 52 | private int writeTimeoutMillis; 53 | 54 | private OkHttpClient realOkClient; 55 | 56 | private OkHttpClient okClient = RxHttpPlugins.getOkHttpClient(); 57 | 58 | protected IConverter converter = RxHttpPlugins.getConverter(); 59 | 60 | protected boolean isAsync = true; 61 | 62 | protected P param; 63 | 64 | public Request request; 65 | 66 | protected RxHttp(P param) { 67 | this.param = param; 68 | } 69 | 70 | public P getParam() { 71 | return param; 72 | } 73 | 74 | public R setParam(P param) { 75 | this.param = param; 76 | return (R)this; 77 | } 78 | 79 | public R connectTimeout(int connectTimeout) { 80 | connectTimeoutMillis = connectTimeout; 81 | return (R)this; 82 | } 83 | 84 | public R readTimeout(int readTimeout) { 85 | readTimeoutMillis = readTimeout; 86 | return (R)this; 87 | } 88 | 89 | public R writeTimeout(int writeTimeout) { 90 | writeTimeoutMillis = writeTimeout; 91 | return (R)this; 92 | } 93 | 94 | public OkHttpClient getOkHttpClient() { 95 | if (realOkClient != null) return realOkClient; 96 | final OkHttpClient okHttpClient = okClient; 97 | OkHttpClient.Builder builder = null; 98 | 99 | if (connectTimeoutMillis != 0) { 100 | if (builder == null) builder = okHttpClient.newBuilder(); 101 | builder.connectTimeout(connectTimeoutMillis, TimeUnit.MILLISECONDS); 102 | } 103 | 104 | if (readTimeoutMillis != 0) { 105 | if (builder == null) builder = okHttpClient.newBuilder(); 106 | builder.readTimeout(readTimeoutMillis, TimeUnit.MILLISECONDS); 107 | } 108 | 109 | if (writeTimeoutMillis != 0) { 110 | if (builder == null) builder = okHttpClient.newBuilder(); 111 | builder.writeTimeout(writeTimeoutMillis, TimeUnit.MILLISECONDS); 112 | } 113 | 114 | if (param.getCacheMode() != CacheMode.ONLY_NETWORK) { 115 | if (builder == null) builder = okHttpClient.newBuilder(); 116 | builder.addInterceptor(new CacheInterceptor(getCacheStrategy())); 117 | } 118 | 119 | realOkClient = builder != null ? builder.build() : okHttpClient; 120 | return realOkClient; 121 | } 122 | 123 | public static void dispose(Disposable disposable) { 124 | if (!isDisposed(disposable)) disposable.dispose(); 125 | } 126 | 127 | public static boolean isDisposed(Disposable disposable) { 128 | return disposable == null || disposable.isDisposed(); 129 | } 130 | 131 | /** 132 | * For example: 133 | * 134 | * ``` 135 | * RxHttp.get("/service/%1$s/...?pageSize=%2$s", 1, 20) 136 | * .asString() 137 | * .subscribe() 138 | * ``` 139 | */ 140 | public static RxHttpNoBodyParam get(String url, Object... formatArgs) { 141 | return new RxHttpNoBodyParam(Param.get(format(url, formatArgs))); 142 | } 143 | 144 | public static RxHttpNoBodyParam head(String url, Object... formatArgs) { 145 | return new RxHttpNoBodyParam(Param.head(format(url, formatArgs))); 146 | } 147 | 148 | public static RxHttpBodyParam postBody(String url, Object... formatArgs) { 149 | return new RxHttpBodyParam(Param.postBody(format(url, formatArgs))); 150 | } 151 | 152 | public static RxHttpBodyParam putBody(String url, Object... formatArgs) { 153 | return new RxHttpBodyParam(Param.putBody(format(url, formatArgs))); 154 | } 155 | 156 | public static RxHttpBodyParam patchBody(String url, Object... formatArgs) { 157 | return new RxHttpBodyParam(Param.patchBody(format(url, formatArgs))); 158 | } 159 | 160 | public static RxHttpBodyParam deleteBody(String url, Object... formatArgs) { 161 | return new RxHttpBodyParam(Param.deleteBody(format(url, formatArgs))); 162 | } 163 | 164 | public static RxHttpFormParam postForm(String url, Object... formatArgs) { 165 | return new RxHttpFormParam(Param.postForm(format(url, formatArgs))); 166 | } 167 | 168 | public static RxHttpFormParam putForm(String url, Object... formatArgs) { 169 | return new RxHttpFormParam(Param.putForm(format(url, formatArgs))); 170 | } 171 | 172 | public static RxHttpFormParam patchForm(String url, Object... formatArgs) { 173 | return new RxHttpFormParam(Param.patchForm(format(url, formatArgs))); 174 | } 175 | 176 | public static RxHttpFormParam deleteForm(String url, Object... formatArgs) { 177 | return new RxHttpFormParam(Param.deleteForm(format(url, formatArgs))); 178 | } 179 | 180 | public static RxHttpJsonParam postJson(String url, Object... formatArgs) { 181 | return new RxHttpJsonParam(Param.postJson(format(url, formatArgs))); 182 | } 183 | 184 | public static RxHttpJsonParam putJson(String url, Object... formatArgs) { 185 | return new RxHttpJsonParam(Param.putJson(format(url, formatArgs))); 186 | } 187 | 188 | public static RxHttpJsonParam patchJson(String url, Object... formatArgs) { 189 | return new RxHttpJsonParam(Param.patchJson(format(url, formatArgs))); 190 | } 191 | 192 | public static RxHttpJsonParam deleteJson(String url, Object... formatArgs) { 193 | return new RxHttpJsonParam(Param.deleteJson(format(url, formatArgs))); 194 | } 195 | 196 | public static RxHttpJsonArrayParam postJsonArray(String url, Object... formatArgs) { 197 | return new RxHttpJsonArrayParam(Param.postJsonArray(format(url, formatArgs))); 198 | } 199 | 200 | public static RxHttpJsonArrayParam putJsonArray(String url, Object... formatArgs) { 201 | return new RxHttpJsonArrayParam(Param.putJsonArray(format(url, formatArgs))); 202 | } 203 | 204 | public static RxHttpJsonArrayParam patchJsonArray(String url, Object... formatArgs) { 205 | return new RxHttpJsonArrayParam(Param.patchJsonArray(format(url, formatArgs))); 206 | } 207 | 208 | public static RxHttpJsonArrayParam deleteJsonArray(String url, Object... formatArgs) { 209 | return new RxHttpJsonArrayParam(Param.deleteJsonArray(format(url, formatArgs))); 210 | } 211 | 212 | public R setUrl(String url) { 213 | param.setUrl(url); 214 | return (R)this; 215 | } 216 | 217 | public R removeAllQuery() { 218 | param.removeAllQuery(); 219 | return (R)this; 220 | } 221 | 222 | public R removeAllQuery(String key) { 223 | param.removeAllQuery(key); 224 | return (R)this; 225 | } 226 | 227 | public R addQuery(String key, Object value) { 228 | param.addQuery(key,value); 229 | return (R)this; 230 | } 231 | 232 | public R setQuery(String key, Object value) { 233 | param.setQuery(key,value); 234 | return (R)this; 235 | } 236 | 237 | public R addEncodedQuery(String key, Object value) { 238 | param.addEncodedQuery(key,value); 239 | return (R)this; 240 | } 241 | 242 | public R setEncodedQuery(String key, Object value) { 243 | param.setEncodedQuery(key,value); 244 | return (R)this; 245 | } 246 | 247 | public R addAllQuery(Map map) { 248 | param.addAllQuery(map); 249 | return (R)this; 250 | } 251 | 252 | public R setAllQuery(Map map) { 253 | param.setAllQuery(map); 254 | return (R)this; 255 | } 256 | 257 | public R addAllEncodedQuery(Map map) { 258 | param.addAllEncodedQuery(map); 259 | return (R)this; 260 | } 261 | 262 | public R setAllEncodedQuery(Map map) { 263 | param.setAllEncodedQuery(map); 264 | return (R)this; 265 | } 266 | 267 | public R addHeader(String line) { 268 | param.addHeader(line); 269 | return (R)this; 270 | } 271 | 272 | public R addHeader(String line, boolean isAdd) { 273 | if(isAdd) { 274 | param.addHeader(line); 275 | } 276 | return (R)this; 277 | } 278 | 279 | /** 280 | * Add a header with the specified name and value. Does validation of header names, allowing non-ASCII values. 281 | */ 282 | public R addNonAsciiHeader(String key, String value) { 283 | param.addNonAsciiHeader(key,value); 284 | return (R)this; 285 | } 286 | 287 | /** 288 | * Set a header with the specified name and value. Does validation of header names, allowing non-ASCII values. 289 | */ 290 | public R setNonAsciiHeader(String key, String value) { 291 | param.setNonAsciiHeader(key,value); 292 | return (R)this; 293 | } 294 | 295 | public R addHeader(String key, String value) { 296 | param.addHeader(key,value); 297 | return (R)this; 298 | } 299 | 300 | public R addHeader(String key, String value, boolean isAdd) { 301 | if(isAdd) { 302 | param.addHeader(key,value); 303 | } 304 | return (R)this; 305 | } 306 | 307 | public R addAllHeader(Map headers) { 308 | param.addAllHeader(headers); 309 | return (R)this; 310 | } 311 | 312 | public R addAllHeader(Headers headers) { 313 | param.addAllHeader(headers); 314 | return (R)this; 315 | } 316 | 317 | public R setHeader(String key, String value) { 318 | param.setHeader(key,value); 319 | return (R)this; 320 | } 321 | 322 | public R setAllHeader(Map headers) { 323 | param.setAllHeader(headers); 324 | return (R)this; 325 | } 326 | 327 | public R setRangeHeader(long startIndex) { 328 | return setRangeHeader(startIndex, -1, false); 329 | } 330 | 331 | public R setRangeHeader(long startIndex, long endIndex) { 332 | return setRangeHeader(startIndex, endIndex, false); 333 | } 334 | 335 | public R setRangeHeader(long startIndex, boolean connectLastProgress) { 336 | return setRangeHeader(startIndex, -1, connectLastProgress); 337 | } 338 | 339 | /** 340 | * 设置断点下载开始/结束位置 341 | * @param startIndex 断点下载开始位置 342 | * @param endIndex 断点下载结束位置,默认为-1,即默认结束位置为文件末尾 343 | * @param connectLastProgress 是否衔接上次的下载进度,该参数仅在带进度断点下载时生效 344 | */ 345 | public R setRangeHeader(long startIndex, long endIndex, boolean connectLastProgress) { 346 | param.setRangeHeader(startIndex, endIndex); 347 | if (connectLastProgress) 348 | param.tag(DownloadOffSize.class, new DownloadOffSize(startIndex)); 349 | return (R) this; 350 | } 351 | 352 | public R removeAllHeader(String key) { 353 | param.removeAllHeader(key); 354 | return (R)this; 355 | } 356 | 357 | public R setHeadersBuilder(Headers.Builder builder) { 358 | param.setHeadersBuilder(builder); 359 | return (R)this; 360 | } 361 | 362 | /** 363 | * 设置单个接口是否需要添加公共参数, 364 | * 即是否回调通过{@link #setOnParamAssembly(Function)}方法设置的接口,默认为true 365 | */ 366 | public R setAssemblyEnabled(boolean enabled) { 367 | param.setAssemblyEnabled(enabled); 368 | return (R)this; 369 | } 370 | 371 | /** 372 | * 设置单个接口是否需要对Http返回的数据进行解码/解密, 373 | * 即是否回调通过{@link #setResultDecoder(Function)}方法设置的接口,默认为true 374 | */ 375 | public R setDecoderEnabled(boolean enabled) { 376 | param.addHeader(Param.DATA_DECRYPT,String.valueOf(enabled)); 377 | return (R)this; 378 | } 379 | 380 | public boolean isAssemblyEnabled() { 381 | return param.isAssemblyEnabled(); 382 | } 383 | 384 | public String getUrl() { 385 | addDefaultDomainIfAbsent(param); 386 | return param.getUrl(); 387 | } 388 | 389 | public String getSimpleUrl() { 390 | return param.getSimpleUrl(); 391 | } 392 | 393 | public String getHeader(String key) { 394 | return param.getHeader(key); 395 | } 396 | 397 | public Headers getHeaders() { 398 | return param.getHeaders(); 399 | } 400 | 401 | public Headers.Builder getHeadersBuilder() { 402 | return param.getHeadersBuilder(); 403 | } 404 | 405 | public R tag(Object tag) { 406 | param.tag(tag); 407 | return (R)this; 408 | } 409 | 410 | public R tag(Class type, T tag) { 411 | param.tag(type,tag); 412 | return (R)this; 413 | } 414 | 415 | public R cacheControl(CacheControl cacheControl) { 416 | param.cacheControl(cacheControl); 417 | return (R)this; 418 | } 419 | 420 | public CacheStrategy getCacheStrategy() { 421 | return param.getCacheStrategy(); 422 | } 423 | 424 | public R setCacheKey(String cacheKey) { 425 | param.setCacheKey(cacheKey); 426 | return (R)this; 427 | } 428 | 429 | public R setCacheValidTime(long cacheValidTime) { 430 | param.setCacheValidTime(cacheValidTime); 431 | return (R)this; 432 | } 433 | 434 | public R setCacheMode(CacheMode cacheMode) { 435 | param.setCacheMode(cacheMode); 436 | return (R)this; 437 | } 438 | 439 | public Response execute() throws IOException { 440 | return newCall().execute(); 441 | } 442 | 443 | public T execute(Parser parser) throws IOException { 444 | return parser.onParse(execute()); 445 | } 446 | 447 | public String executeString() throws IOException { 448 | return executeClass(String.class); 449 | } 450 | 451 | public List executeList(Class type) throws IOException { 452 | Type tTypeList = ParameterizedTypeImpl.get(List.class, type); 453 | return execute(new SimpleParser>(tTypeList)); 454 | } 455 | 456 | public T executeClass(Class type) throws IOException { 457 | return execute(new SimpleParser(type)); 458 | } 459 | 460 | @Override 461 | public final Call newCall() { 462 | Request request = buildRequest(); 463 | OkHttpClient okClient = getOkHttpClient(); 464 | return okClient.newCall(request); 465 | } 466 | 467 | public final Request buildRequest() { 468 | boolean debug = LogUtil.isDebug(); 469 | if (request == null) { 470 | doOnStart(); 471 | request = param.buildRequest(); 472 | if (debug) 473 | LogUtil.log(request, getOkHttpClient().cookieJar()); 474 | } 475 | if (debug) { 476 | request = request.newBuilder() 477 | .tag(LogTime.class, new LogTime()) 478 | .build(); 479 | } 480 | return request; 481 | } 482 | 483 | /** 484 | * 请求开始前内部调用,用于添加默认域名等操作 485 | */ 486 | private final void doOnStart() { 487 | setConverter(param); 488 | addDefaultDomainIfAbsent(param); 489 | } 490 | 491 | /** 492 | * @deprecated please user {@link #setSync()} instead 493 | */ 494 | @Deprecated 495 | public R subscribeOnCurrent() { 496 | return setSync(); 497 | } 498 | 499 | /** 500 | * sync request 501 | */ 502 | public R setSync() { 503 | isAsync = false; 504 | return (R)this; 505 | } 506 | 507 | public Observable asParser(Parser parser, Scheduler scheduler, 508 | Consumer progressConsumer) { 509 | ObservableCall observableCall; 510 | if (isAsync) { 511 | observableCall = new ObservableCallEnqueue(this); 512 | } else { 513 | observableCall = new ObservableCallExecute(this); 514 | } 515 | return observableCall.asParser(parser, scheduler, progressConsumer); 516 | } 517 | 518 | /** 519 | * 给Param设置转换器,此方法会在请求发起前,被RxHttp内部调用 520 | */ 521 | private R setConverter(P param) { 522 | param.tag(IConverter.class,converter); 523 | return (R)this; 524 | } 525 | 526 | public R setOkClient(OkHttpClient okClient) { 527 | if (okClient == null) 528 | throw new IllegalArgumentException("okClient can not be null"); 529 | this.okClient = okClient; 530 | return (R)this; 531 | } 532 | 533 | /** 534 | * 给Param设置默认域名(如何缺席的话),此方法会在请求发起前,被RxHttp内部调用 535 | */ 536 | private P addDefaultDomainIfAbsent(P param) { 537 | String newUrl = addDomainIfAbsent(param.getSimpleUrl(), Url.baseUrl); 538 | param.setUrl(newUrl); 539 | return param; 540 | } 541 | 542 | public R setDomainToUpdateIfAbsent() { 543 | String newUrl = addDomainIfAbsent(param.getSimpleUrl(), Url.update); 544 | param.setUrl(newUrl); 545 | return (R)this; 546 | } 547 | 548 | private static String addDomainIfAbsent(String url, String domain) { 549 | if (url.startsWith("http")) return url; 550 | if (url.startsWith("/")) { 551 | if (domain.endsWith("/")) 552 | return domain + url.substring(1); 553 | else 554 | return domain + url; 555 | } else if (domain.endsWith("/")) { 556 | return domain + url; 557 | } else { 558 | return domain + "/" + url; 559 | } 560 | } 561 | 562 | /** 563 | * Returns a formatted string using the specified format string and arguments. 564 | */ 565 | private static String format(String url, Object... formatArgs) { 566 | return formatArgs == null || formatArgs.length == 0 ? url : String.format(url, formatArgs); 567 | } 568 | } 569 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttp.kt: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param 2 | 3 | import kotlin.Unit 4 | import kotlinx.coroutines.CoroutineScope 5 | import kotlinx.coroutines.launch 6 | import rxhttp.wrapper.entity.Progress 7 | import rxhttp.wrapper.parse.SimpleParser 8 | 9 | inline fun RxHttp<*, *>.executeList() = executeClass>() 10 | 11 | inline fun RxHttp<*, *>.executeClass() = execute(object : SimpleParser() {}) 12 | 13 | inline fun BaseRxHttp.asList() = asClass>() 14 | 15 | inline fun BaseRxHttp.asMap() = asClass>() 16 | 17 | inline fun BaseRxHttp.asClass() = asParser(object : SimpleParser() {}) 18 | 19 | /** 20 | * 调用此方法监听上传进度 21 | * @param coroutine CoroutineScope对象,用于开启协程回调进度,进度回调所在线程取决于协程所在线程 22 | * @param progress 进度回调 23 | */ 24 | fun

, R : RxHttpAbstractBodyParam> RxHttpAbstractBodyParam.upload(coroutine: CoroutineScope, progress: suspend (Progress) -> Unit): R { 26 | param.setProgressCallback { 27 | coroutine.launch { progress(it) } 28 | } 29 | return this as R 30 | } 31 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttpBodyParam.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import android.content.Context; 4 | import android.net.Uri; 5 | 6 | import rxhttp.wrapper.annotations.Nullable; 7 | import rxhttp.wrapper.param.BodyParam; 8 | 9 | import java.io.File; 10 | 11 | import okhttp3.MediaType; 12 | import okhttp3.RequestBody; 13 | import okio.ByteString; 14 | 15 | /** 16 | * Github 17 | * https://github.com/liujingxing/RxHttp 18 | * https://github.com/liujingxing/RxLife 19 | */ 20 | public class RxHttpBodyParam extends RxHttpAbstractBodyParam { 21 | public RxHttpBodyParam(BodyParam param) { 22 | super(param); 23 | } 24 | 25 | public RxHttpBodyParam setBody(RequestBody requestBody) { 26 | param.setBody(requestBody); 27 | return this; 28 | } 29 | 30 | public RxHttpBodyParam setBody(String content, @Nullable MediaType mediaType) { 31 | param.setBody(content, mediaType); 32 | return this; 33 | } 34 | 35 | public RxHttpBodyParam setBody(ByteString content, @Nullable MediaType mediaType) { 36 | param.setBody(content, mediaType); 37 | return this; 38 | } 39 | 40 | public RxHttpBodyParam setBody(byte[] content, @Nullable MediaType mediaType) { 41 | param.setBody(content, mediaType); 42 | return this; 43 | } 44 | 45 | public RxHttpBodyParam setBody(byte[] content, @Nullable MediaType mediaType, int offset, int byteCount) { 46 | param.setBody(content, mediaType, offset, byteCount); 47 | return this; 48 | } 49 | 50 | public RxHttpBodyParam setBody(File file) { 51 | param.setBody(file); 52 | return this; 53 | } 54 | 55 | public RxHttpBodyParam setBody(File file, @Nullable MediaType mediaType) { 56 | param.setBody(file, mediaType); 57 | return this; 58 | } 59 | 60 | public RxHttpBodyParam setBody(Uri uri, Context context) { 61 | param.setBody(uri, context); 62 | return this; 63 | } 64 | 65 | public RxHttpBodyParam setBody(Uri uri, Context context, @Nullable MediaType contentType) { 66 | param.setBody(uri, context, contentType); 67 | return this; 68 | } 69 | 70 | public RxHttpBodyParam setJsonBody(Object object) { 71 | param.setJsonBody(object); 72 | return this; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttpFormParam.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import android.content.Context; 4 | import android.net.Uri; 5 | 6 | import java.io.File; 7 | import java.util.List; 8 | import java.util.Map; 9 | import java.util.Map.Entry; 10 | 11 | import okhttp3.Headers; 12 | import okhttp3.MediaType; 13 | import okhttp3.MultipartBody.Part; 14 | import okhttp3.RequestBody; 15 | import rxhttp.wrapper.annotations.NonNull; 16 | import rxhttp.wrapper.annotations.Nullable; 17 | import rxhttp.wrapper.entity.UpFile; 18 | import rxhttp.wrapper.param.FormParam; 19 | import rxhttp.wrapper.utils.UriUtil; 20 | 21 | /** 22 | * Github 23 | * https://github.com/liujingxing/RxHttp 24 | * https://github.com/liujingxing/RxLife 25 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/FAQ 26 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/更新日志 27 | */ 28 | public class RxHttpFormParam extends RxHttpAbstractBodyParam { 29 | public RxHttpFormParam(FormParam param) { 30 | super(param); 31 | } 32 | 33 | public RxHttpFormParam add(String key, Object value) { 34 | param.add(key,value); 35 | return this; 36 | } 37 | 38 | public RxHttpFormParam add(String key, Object value, boolean isAdd) { 39 | if(isAdd) { 40 | param.add(key,value); 41 | } 42 | return this; 43 | } 44 | 45 | public RxHttpFormParam addAll(Map map) { 46 | param.addAll(map); 47 | return this; 48 | } 49 | 50 | public RxHttpFormParam addEncoded(String key, Object value) { 51 | param.addEncoded(key, value); 52 | return this; 53 | } 54 | 55 | public RxHttpFormParam addAllEncoded(@NonNull Map map) { 56 | param.addAllEncoded(map); 57 | return this; 58 | } 59 | 60 | public RxHttpFormParam removeAllBody() { 61 | param.removeAllBody(); 62 | return this; 63 | } 64 | 65 | public RxHttpFormParam removeAllBody(String key) { 66 | param.removeAllBody(key); 67 | return this; 68 | } 69 | 70 | public RxHttpFormParam set(String key, Object value) { 71 | param.set(key, value); 72 | return this; 73 | } 74 | 75 | public RxHttpFormParam setEncoded(String key, Object value) { 76 | param.setEncoded(key, value); 77 | return this; 78 | } 79 | 80 | /** 81 | * @deprecated please user {@link #addFile(String, File)} instead 82 | */ 83 | @Deprecated 84 | public RxHttpFormParam add(String key, File file) { 85 | param.addFile(key, file); 86 | return this; 87 | } 88 | 89 | public RxHttpFormParam addFile(String key, File file) { 90 | param.addFile(key, file); 91 | return this; 92 | } 93 | 94 | public RxHttpFormParam addFile(String key, String filePath) { 95 | param.addFile(key, filePath); 96 | return this; 97 | } 98 | 99 | public RxHttpFormParam addFile(String key, String filename, String filePath) { 100 | param.addFile(key, filename, filePath); 101 | return this; 102 | } 103 | 104 | public RxHttpFormParam addFile(String key, String filename, File file) { 105 | param.addFile(key, filename, file); 106 | return this; 107 | } 108 | 109 | public RxHttpFormParam addFile(UpFile file) { 110 | param.addFile(file); 111 | return this; 112 | } 113 | 114 | /** 115 | * @deprecated please user {@link #addFiles(List)} instead 116 | */ 117 | @Deprecated 118 | public RxHttpFormParam addFile(List fileList) { 119 | return addFiles(fileList); 120 | } 121 | 122 | /** 123 | * @deprecated please user {@link #addFiles(String, List)} instead 124 | */ 125 | @Deprecated 126 | public RxHttpFormParam addFile(String key, List fileList) { 127 | return addFiles(key, fileList); 128 | } 129 | 130 | public RxHttpFormParam addFiles(List fileList) { 131 | param.addFiles(fileList); 132 | return this; 133 | } 134 | 135 | public RxHttpFormParam addFiles(Map fileMap) { 136 | param.addFiles(fileMap); 137 | return this; 138 | } 139 | 140 | public RxHttpFormParam addFiles(String key, List fileList) { 141 | param.addFiles(key, fileList); 142 | return this; 143 | } 144 | 145 | public RxHttpFormParam addPart(@Nullable MediaType contentType, byte[] content) { 146 | param.addPart(contentType, content); 147 | return this; 148 | } 149 | 150 | public RxHttpFormParam addPart(@Nullable MediaType contentType, byte[] content, int offset, 151 | int byteCount) { 152 | param.addPart(contentType, content, offset, byteCount); 153 | return this; 154 | } 155 | 156 | public RxHttpFormParam addPart(Context context, Uri uri) { 157 | param.addPart(UriUtil.asRequestBody(uri, context)); 158 | return this; 159 | } 160 | 161 | public RxHttpFormParam addPart(Context context, String key, Uri uri) { 162 | param.addPart(UriUtil.asPart(uri, context, key)); 163 | return this; 164 | } 165 | 166 | public RxHttpFormParam addPart(Context context, String key, String fileName, Uri uri) { 167 | param.addPart(UriUtil.asPart(uri, context, key, fileName)); 168 | return this; 169 | } 170 | 171 | public RxHttpFormParam addPart(Context context, Uri uri, @Nullable MediaType contentType) { 172 | param.addPart(UriUtil.asRequestBody(uri, context, contentType)); 173 | return this; 174 | } 175 | 176 | public RxHttpFormParam addPart(Context context, String key, Uri uri, 177 | @Nullable MediaType contentType) { 178 | param.addPart(UriUtil.asPart(uri, context, key, null, contentType)); 179 | return this; 180 | } 181 | 182 | public RxHttpFormParam addPart(Context context, String key, String filename, Uri uri, 183 | @Nullable MediaType contentType) { 184 | param.addPart(UriUtil.asPart(uri, context, key, filename, contentType)); 185 | return this; 186 | } 187 | 188 | public RxHttpFormParam addParts(Context context, Map uriMap) { 189 | for (Entry entry : uriMap.entrySet()) { 190 | addPart(context, entry.getKey(), entry.getValue()); 191 | } 192 | return this; 193 | } 194 | 195 | public RxHttpFormParam addParts(Context context, List uris) { 196 | for (Uri uri : uris) { 197 | addPart(context, uri); 198 | } 199 | return this; 200 | } 201 | 202 | public RxHttpFormParam addParts(Context context, String key, List uris) { 203 | for (Uri uri : uris) { 204 | addPart(context, key, uri); 205 | } 206 | return this; 207 | } 208 | 209 | public RxHttpFormParam addParts(Context context, List uris, 210 | @Nullable MediaType contentType) { 211 | for (Uri uri : uris) { 212 | addPart(context, uri, contentType); 213 | } 214 | return this; 215 | } 216 | 217 | public RxHttpFormParam addParts(Context context, String key, List uris, 218 | @Nullable MediaType contentType) { 219 | for (Uri uri : uris) { 220 | addPart(context, key, uri, contentType); 221 | } 222 | return this; 223 | } 224 | 225 | public RxHttpFormParam addPart(Part part) { 226 | param.addPart(part); 227 | return this; 228 | } 229 | 230 | public RxHttpFormParam addPart(RequestBody requestBody) { 231 | param.addPart(requestBody); 232 | return this; 233 | } 234 | 235 | public RxHttpFormParam addPart(Headers headers, RequestBody requestBody) { 236 | param.addPart(headers, requestBody); 237 | return this; 238 | } 239 | 240 | public RxHttpFormParam addFormDataPart(String key, String fileName, RequestBody requestBody) { 241 | param.addFormDataPart(key, fileName, requestBody); 242 | return this; 243 | } 244 | 245 | //Set content-type to multipart/form-data 246 | public RxHttpFormParam setMultiForm() { 247 | param.setMultiForm(); 248 | return this; 249 | } 250 | 251 | //Set content-type to multipart/mixed 252 | public RxHttpFormParam setMultiMixed() { 253 | param.setMultiMixed(); 254 | return this; 255 | } 256 | 257 | //Set content-type to multipart/alternative 258 | public RxHttpFormParam setMultiAlternative() { 259 | param.setMultiAlternative(); 260 | return this; 261 | } 262 | 263 | //Set content-type to multipart/digest 264 | public RxHttpFormParam setMultiDigest() { 265 | param.setMultiDigest(); 266 | return this; 267 | } 268 | 269 | //Set content-type to multipart/parallel 270 | public RxHttpFormParam setMultiParallel() { 271 | param.setMultiParallel(); 272 | return this; 273 | } 274 | 275 | //Set the MIME type 276 | public RxHttpFormParam setMultiType(MediaType multiType) { 277 | param.setMultiType(multiType); 278 | return this; 279 | } 280 | } 281 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttpJsonArrayParam.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import com.google.gson.JsonArray; 4 | import com.google.gson.JsonObject; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import rxhttp.wrapper.param.JsonArrayParam; 10 | 11 | /** 12 | * Github 13 | * https://github.com/liujingxing/RxHttp 14 | * https://github.com/liujingxing/RxLife 15 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/FAQ 16 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/更新日志 17 | */ 18 | public class RxHttpJsonArrayParam extends RxHttpAbstractBodyParam { 19 | public RxHttpJsonArrayParam(JsonArrayParam param) { 20 | super(param); 21 | } 22 | 23 | public RxHttpJsonArrayParam add(String key, Object value) { 24 | param.add(key,value); 25 | return this; 26 | } 27 | 28 | public RxHttpJsonArrayParam add(String key, Object value, boolean isAdd) { 29 | if(isAdd) { 30 | param.add(key,value); 31 | } 32 | return this; 33 | } 34 | 35 | public RxHttpJsonArrayParam addAll(Map map) { 36 | param.addAll(map); 37 | return this; 38 | } 39 | 40 | public RxHttpJsonArrayParam add(Object object) { 41 | param.add(object); 42 | return this; 43 | } 44 | 45 | public RxHttpJsonArrayParam addAll(List list) { 46 | param.addAll(list); 47 | return this; 48 | } 49 | 50 | /** 51 | * 添加多个对象,将字符串转JsonElement对象,并根据不同类型,执行不同操作,可输入任意非空字符串 52 | */ 53 | public RxHttpJsonArrayParam addAll(String jsonElement) { 54 | param.addAll(jsonElement); 55 | return this; 56 | } 57 | 58 | public RxHttpJsonArrayParam addAll(JsonArray jsonArray) { 59 | param.addAll(jsonArray); 60 | return this; 61 | } 62 | 63 | /** 64 | * 将Json对象里面的key-value逐一取出,添加到Json数组中,成为单独的对象 65 | */ 66 | public RxHttpJsonArrayParam addAll(JsonObject jsonObject) { 67 | param.addAll(jsonObject); 68 | return this; 69 | } 70 | 71 | public RxHttpJsonArrayParam addJsonElement(String jsonElement) { 72 | param.addJsonElement(jsonElement); 73 | return this; 74 | } 75 | 76 | /** 77 | * 添加一个JsonElement对象(Json对象、json数组等) 78 | */ 79 | public RxHttpJsonArrayParam addJsonElement(String key, String jsonElement) { 80 | param.addJsonElement(key, jsonElement); 81 | return this; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttpJsonParam.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import com.google.gson.JsonObject; 4 | 5 | import java.util.Map; 6 | 7 | import rxhttp.wrapper.param.JsonParam; 8 | /** 9 | * Github 10 | * https://github.com/liujingxing/RxHttp 11 | * https://github.com/liujingxing/RxLife 12 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/FAQ 13 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/更新日志 14 | */ 15 | public class RxHttpJsonParam extends RxHttpAbstractBodyParam { 16 | public RxHttpJsonParam(JsonParam param) { 17 | super(param); 18 | } 19 | 20 | public RxHttpJsonParam add(String key, Object value) { 21 | param.add(key,value); 22 | return this; 23 | } 24 | 25 | public RxHttpJsonParam add(String key, Object value, boolean isAdd) { 26 | if(isAdd) { 27 | param.add(key,value); 28 | } 29 | return this; 30 | } 31 | 32 | public RxHttpJsonParam addAll(Map map) { 33 | param.addAll(map); 34 | return this; 35 | } 36 | 37 | /** 38 | * 将Json对象里面的key-value逐一取出,添加到另一个Json对象中, 39 | * 输入非Json对象将抛出{@link IllegalStateException}异常 40 | */ 41 | public RxHttpJsonParam addAll(String jsonObject) { 42 | param.addAll(jsonObject); 43 | return this; 44 | } 45 | 46 | /** 47 | * 将Json对象里面的key-value逐一取出,添加到另一个Json对象中 48 | */ 49 | public RxHttpJsonParam addAll(JsonObject jsonObject) { 50 | param.addAll(jsonObject); 51 | return this; 52 | } 53 | 54 | /** 55 | * 添加一个JsonElement对象(Json对象、json数组等) 56 | */ 57 | public RxHttpJsonParam addJsonElement(String key, String jsonElement) { 58 | param.addJsonElement(key, jsonElement); 59 | return this; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/build/generated/source/kapt/debug/rxhttp/wrapper/param/RxHttpNoBodyParam.java: -------------------------------------------------------------------------------- 1 | package rxhttp.wrapper.param; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | 6 | import rxhttp.wrapper.annotations.NonNull; 7 | import rxhttp.wrapper.param.NoBodyParam; 8 | 9 | /** 10 | * Github 11 | * https://github.com/liujingxing/RxHttp 12 | * https://github.com/liujingxing/RxLife 13 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/FAQ 14 | * https://github.com/liujingxing/okhttp-RxHttp/wiki/更新日志 15 | */ 16 | public class RxHttpNoBodyParam extends RxHttp { 17 | public RxHttpNoBodyParam(NoBodyParam param) { 18 | super(param); 19 | } 20 | 21 | public RxHttpNoBodyParam add(String key, Object value) { 22 | return addQuery(key, value); 23 | } 24 | 25 | public RxHttpNoBodyParam add(String key, Object value, boolean isAdd) { 26 | if (isAdd) { 27 | addQuery(key, value); 28 | } 29 | return this; 30 | } 31 | 32 | public RxHttpNoBodyParam addAll(Map map) { 33 | return addAllQuery(map); 34 | } 35 | 36 | public RxHttpNoBodyParam addEncoded(String key, Object value) { 37 | return addEncodedQuery(key, value); 38 | } 39 | 40 | public RxHttpNoBodyParam addAllEncoded(@NonNull Map map) { 41 | return addAllEncodedQuery(map); 42 | } 43 | 44 | public RxHttpNoBodyParam set(String key, Object value) { 45 | return setQuery(key, value); 46 | } 47 | 48 | public RxHttpNoBodyParam setEncoded(String key, Object value) { 49 | return setEncodedQuery(key, value); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/coroutine/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.coroutine 2 | 3 | import android.os.Bundle 4 | import android.view.View 5 | import androidx.appcompat.app.AppCompatActivity 6 | import androidx.databinding.DataBindingUtil 7 | import androidx.lifecycle.rxLifeScope 8 | import com.example.coroutine.databinding.ActivityMainBinding 9 | import kotlinx.coroutines.Dispatchers 10 | import rxhttp.RxHttpPlugins 11 | import rxhttp.toDownload 12 | import rxhttp.wrapper.param.RxHttp 13 | 14 | class MainActivity : AppCompatActivity() { 15 | 16 | private lateinit var mBinding: ActivityMainBinding 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) 21 | RxHttpPlugins.init(null).setDebug(true) 22 | } 23 | 24 | //文件下载,带进度 25 | fun downloadAndProgress(view: View) { 26 | val launch = rxLifeScope.launch({ 27 | //文件存储路径 28 | val destPath = externalCacheDir.toString() + "/" + System.currentTimeMillis() + ".apk" 29 | val result = RxHttp.get("/miaolive/Miaolive.apk") 30 | .setDomainToUpdateIfAbsent() //使用指定的域名 31 | .toDownload(destPath, Dispatchers.Main) { 32 | //下载进度回调,0-100,仅在进度有更新时才会回调,最多回调101次,最后一次回调文件存储路径 33 | val currentProgress = it.progress //当前进度 0-100 34 | val currentSize = it.currentSize //当前已下载的字节大小 35 | val totalSize = it.totalSize //要下载的总字节大小 36 | mBinding.tvResult.append(it.toString()) 37 | } 38 | .await() 39 | mBinding.tvResult.append(result) 40 | }, { 41 | mBinding.tvResult.append("\n${it.message}") 42 | }) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/coroutine/Url.kt: -------------------------------------------------------------------------------- 1 | package com.example.coroutine 2 | 3 | import rxhttp.wrapper.annotation.DefaultDomain 4 | import rxhttp.wrapper.annotation.Domain 5 | 6 | /** 7 | * User: ljx 8 | * Date: 2020/4/19 9 | * Time: 13:33 10 | */ 11 | object Url { 12 | 13 | @Domain(name = "Update") 14 | const val update = "http://update.9158.com" 15 | 16 | @DefaultDomain //设置为默认域名 17 | const val baseUrl = "https://www.wanandroid.com/" 18 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 11 |