From a7123b115e037ce13ee8d089f7d8ce15674b8d47 Mon Sep 17 00:00:00 2001 From: zeropercenthappy Date: Wed, 9 Jan 2019 11:08:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5ProgressManager=E5=8F=AF?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E4=B8=8A=E4=BC=A0=E3=80=81=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E8=BF=9B=E5=BA=A6=E7=9B=91=E5=90=AC=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86sample=20release=201.0.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- retrofitLib/build.gradle | 3 +- .../retrofitutil/RetrofitBuilder.kt | 3 +- .../retrofitutilsample/MainActivity.kt | 81 +++++++++++++++++-- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 49462bb..2d1587b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Step 2. Add the dependency ``` dependencies { - implementation 'com.github.zeropercenthappy:RetrofitUtils:1.0.3' + implementation 'com.github.zeropercenthappy:RetrofitUtils:1.0.4' } ``` diff --git a/retrofitLib/build.gradle b/retrofitLib/build.gradle index 2c00b9f..31523c8 100644 --- a/retrofitLib/build.gradle +++ b/retrofitLib/build.gradle @@ -11,7 +11,7 @@ android { minSdkVersion 19 targetSdkVersion 28 versionCode 3 - versionName "1.0.3" + versionName "1.0.4" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -41,6 +41,7 @@ dependencies { api "com.squareup.retrofit2:retrofit:$retrofit_version" api "com.squareup.retrofit2:converter-gson:$retrofit_version" api 'com.squareup.okhttp3:logging-interceptor:3.8.0' + api 'me.jessyan:progressmanager:1.5.0' } task sourcesJar(type: Jar) { diff --git a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt index 6ad0b93..af6e626 100644 --- a/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt +++ b/retrofitLib/src/main/java/com/zeropercenthappy/retrofitutil/RetrofitBuilder.kt @@ -2,6 +2,7 @@ package com.zeropercenthappy.retrofitutil import android.content.Context import android.text.TextUtils +import me.jessyan.progressmanager.ProgressManager import okhttp3.Interceptor import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor @@ -19,7 +20,7 @@ class RetrofitBuilder { private var connectTimeoutMs: Long = 10_000 private var readTimeoutMs: Long = 10_000 private var writeTimeoutMs: Long = 10_000 - private val okHttpClientBuilder = OkHttpClient.Builder() + private val okHttpClientBuilder = ProgressManager.getInstance().with(OkHttpClient.Builder()) fun baseUrl(baseUrl: String): RetrofitBuilder { this.baseUrl = baseUrl diff --git a/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt b/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt index 5396093..82228ca 100644 --- a/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt +++ b/sample/src/main/java/com/zeropercenthappy/retrofitutilsample/MainActivity.kt @@ -12,16 +12,22 @@ import com.zeropercenthappy.retrofitutilsample.pojo.* import com.zeropercenthappy.utilslibrary.utils.CacheUtils import com.zeropercenthappy.utilslibrary.utils.FileUtils import kotlinx.android.synthetic.main.activity_main.* +import me.jessyan.progressmanager.ProgressListener +import me.jessyan.progressmanager.ProgressManager +import me.jessyan.progressmanager.body.ProgressInfo import okhttp3.FormBody import okhttp3.ResponseBody import okhttp3.logging.HttpLoggingInterceptor +import org.jetbrains.anko.AnkoLogger +import org.jetbrains.anko.error +import org.jetbrains.anko.info import retrofit2.Call import retrofit2.Callback import retrofit2.Response import java.io.File import java.util.* -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), AnkoLogger { private lateinit var extraTestParamMap: Map @@ -51,9 +57,17 @@ class MainActivity : AppCompatActivity() { val login = kalleApi.login("guest", "123456") login.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { + if (call.isCanceled) { + // cancel + } else { + // fail + } } override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful && response.body() != null) { + // success + } } }) } @@ -67,9 +81,17 @@ class MainActivity : AppCompatActivity() { val get = kalleApi.get("guest", "25") get.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { + if (call.isCanceled) { + // cancel + } else { + // fail + } } override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful && response.body() != null) { + // success + } } }) } @@ -87,9 +109,17 @@ class MainActivity : AppCompatActivity() { post.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { + if (call.isCanceled) { + // cancel + } else { + // fail + } } override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful && response.body() != null) { + // success + } } }) } @@ -121,30 +151,58 @@ class MainActivity : AppCompatActivity() { val uploadFile = kalleApi.uploadFile(name, age, partList) uploadFile.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { + if (call.isCanceled) { + // cancel + } else { + // fail + } } override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful && response.body() != null) { + // success + } } }) } private fun download() { + val fileUrl = "http://cdn.aixifan.com/downloads/AcFun-portal-release-5.7.0.575-575.apk" val retrofit = RetrofitBuilder() .baseUrl(KalleUrl.BASE_URL) .build(this) val kalleApi = retrofit.create(IKalleApi::class.java) -// val downloadFile = kalleApi.downloadFile("upload/1527220017003e5258758-d4a4-495d-bd07-1eb3a6633f39.jpg") - val downloadFile = kalleApi.downloadFile("http://cdn.aixifan.com/downloads/AcFun-portal-release-5.7.0.575-575.apk") + val downloadFile = kalleApi.downloadFile(fileUrl) + //progress + ProgressManager.getInstance().addResponseListener(fileUrl, object : ProgressListener { + override fun onProgress(progressInfo: ProgressInfo) { + info("progress:${progressInfo.percent}%") + } + + override fun onError(id: Long, e: Exception?) { + e?.printStackTrace() + error(e?.localizedMessage) + } + }) + // downloadFile.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { - + if (call.isCanceled) { + // cancel + } else { + // fail + } } override fun onResponse(call: Call, response: Response) { - val cacheFile = CacheUtils.createFormatedCacheFile(this@MainActivity, "apk") - if (cacheFile != null && response.body() != null) { - FileUtils.writeFileByIS(cacheFile, response.body()!!.byteStream(), false) + // download completely + if (response.isSuccessful && response.body() != null) { + val cacheFile = CacheUtils.createFormatedCacheFile(this@MainActivity, "apk") + if (cacheFile != null) { + FileUtils.writeFileByIS(cacheFile, response.body()!!.byteStream(), false) + } } + } }) } @@ -158,10 +216,17 @@ class MainActivity : AppCompatActivity() { val postJson = kalleApi.postJson(simpleBean) postJson.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { + if (call.isCanceled) { + // cancel + } else { + // fail + } } override fun onResponse(call: Call, response: Response) { - val result = response.body()?.string() + if (response.isSuccessful && response.body() != null) { + // success + } } }) }