Skip to content

Commit

Permalink
引入ProgressManager可作为上传、下载进度监听
Browse files Browse the repository at this point in the history
优化了sample
release 1.0.4
  • Loading branch information
zeropercenthappy committed Jan 9, 2019
1 parent fd6f376 commit a7123b1
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
3 changes: 2 additions & 1 deletion retrofitLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>

Expand Down Expand Up @@ -51,9 +57,17 @@ class MainActivity : AppCompatActivity() {
val login = kalleApi.login("guest", "123456")
login.enqueue(object : Callback<LoginBean> {
override fun onFailure(call: Call<LoginBean>, t: Throwable) {
if (call.isCanceled) {
// cancel
} else {
// fail
}
}

override fun onResponse(call: Call<LoginBean>, response: Response<LoginBean>) {
if (response.isSuccessful && response.body() != null) {
// success
}
}
})
}
Expand All @@ -67,9 +81,17 @@ class MainActivity : AppCompatActivity() {
val get = kalleApi.get("guest", "25")
get.enqueue(object : Callback<GetBean> {
override fun onFailure(call: Call<GetBean>, t: Throwable) {
if (call.isCanceled) {
// cancel
} else {
// fail
}
}

override fun onResponse(call: Call<GetBean>, response: Response<GetBean>) {
if (response.isSuccessful && response.body() != null) {
// success
}
}
})
}
Expand All @@ -87,9 +109,17 @@ class MainActivity : AppCompatActivity() {

post.enqueue(object : Callback<PostBean> {
override fun onFailure(call: Call<PostBean>, t: Throwable) {
if (call.isCanceled) {
// cancel
} else {
// fail
}
}

override fun onResponse(call: Call<PostBean>, response: Response<PostBean>) {
if (response.isSuccessful && response.body() != null) {
// success
}
}
})
}
Expand Down Expand Up @@ -121,30 +151,58 @@ class MainActivity : AppCompatActivity() {
val uploadFile = kalleApi.uploadFile(name, age, partList)
uploadFile.enqueue(object : Callback<UploadBean> {
override fun onFailure(call: Call<UploadBean>, t: Throwable) {
if (call.isCanceled) {
// cancel
} else {
// fail
}
}

override fun onResponse(call: Call<UploadBean>, response: Response<UploadBean>) {
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<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {

if (call.isCanceled) {
// cancel
} else {
// fail
}
}

override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
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)
}
}

}
})
}
Expand All @@ -158,10 +216,17 @@ class MainActivity : AppCompatActivity() {
val postJson = kalleApi.postJson(simpleBean)
postJson.enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
if (call.isCanceled) {
// cancel
} else {
// fail
}
}

override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
val result = response.body()?.string()
if (response.isSuccessful && response.body() != null) {
// success
}
}
})
}
Expand Down

0 comments on commit a7123b1

Please sign in to comment.