Skip to content

Commit

Permalink
转kotlin代码
Browse files Browse the repository at this point in the history
  • Loading branch information
陈力 committed May 22, 2023
1 parent 75c4cd8 commit 5c9c3b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
27 changes: 11 additions & 16 deletions app/src/main/java/com/cl/test/app/AppApplication.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.cl.test.app;
package com.cl.test.app

import android.app.Application;
import android.app.Application
import com.hjq.toast.Toaster

import com.hjq.toast.Toaster;
class AppApplication : Application() {


public class AppApplication extends Application {


private static Application sInstance;


@Override
public void onCreate() {
super.onCreate();
override fun onCreate() {
super.onCreate()

// 初始化 Toast 框架
Toaster.init(this);
Toaster.init(this)
}


}
companion object {
private val sInstance: Application? = null
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/com/cl/test/net/NetworkApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ class NetworkApi : BaseNetworkApi() {
override fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder {


//下面是4.0.0版本的最新方法
//下面是4.0.0版本的最新方法
val httpLoggingInterceptor = HttpLoggingInterceptor { message ->
Log.e(
"下一页网络日志",
"XiaYiYe5:$message"
"网络日志", message
)
}
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.maxvision.mvvm.ext.download

import android.os.Looper
import com.maxvision.mvvm.ext.util.logi
import com.maxvision.mvvm.util.HttpsCerUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import com.maxvision.mvvm.ext.util.logi
import com.maxvision.mvvm.util.HttpsCerUtils
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.io.File
Expand All @@ -19,7 +19,9 @@ import java.util.concurrent.TimeUnit
* 描述 :下载管理类
*/
object DownLoadManager {
private val retrofitBuilder by lazy {

/** 忽略https模式 */
private val retrofitBuilderHttps by lazy {
Retrofit.Builder()
.baseUrl("https://www.baidu.com")
.client(
Expand All @@ -30,13 +32,27 @@ object DownLoadManager {
).build()
}

/** 正常http模式 */
private val retrofitBuilder by lazy {
Retrofit.Builder()
.baseUrl("https://www.baidu.com")
.client(
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS).build()
).build()
}


/**
*开始下载
* @param tag String 标识
* @param url String 下载的url
* @param savePath String 保存的路径
* @param saveName String 保存的名字
* @param reDownload Boolean 如果文件已存在是否需要重新下载 默认不需要重新下载
* @param whetherHttps Boolean 默认不开启忽略https模式
* @param loadListener OnDownLoadListener
*/
suspend fun downLoad(
Expand All @@ -45,10 +61,11 @@ object DownLoadManager {
savePath: String,
saveName: String,
reDownload: Boolean = false,
whetherHttps: Boolean=false,
loadListener: OnDownLoadListener
) {
withContext(Dispatchers.IO) {
doDownLoad(tag, url, savePath, saveName, reDownload, loadListener, this)
doDownLoad(tag, url, savePath, saveName, reDownload,whetherHttps, loadListener, this)
}
}

Expand Down Expand Up @@ -102,6 +119,7 @@ object DownLoadManager {
* @param savePath String 保存的路径
* @param saveName String 保存的名字
* @param reDownload Boolean 如果文件已存在是否需要重新下载 默认不需要重新下载
* @param whetherHttps Boolean 是否是https模式
* @param loadListener OnDownLoadListener
* @param coroutineScope CoroutineScope 上下文
*/
Expand All @@ -111,6 +129,7 @@ object DownLoadManager {
savePath: String,
saveName: String,
reDownload: Boolean,
whetherHttps : Boolean,
loadListener: OnDownLoadListener,
coroutineScope: CoroutineScope
) {
Expand Down Expand Up @@ -160,8 +179,15 @@ object DownLoadManager {
withContext(Dispatchers.Main) {
loadListener.onDownLoadPrepare(key = tag)
}
val response = retrofitBuilder.create(DownLoadService::class.java)
.downloadFile("bytes=$currentLength-", url)
val response = if (whetherHttps) {
retrofitBuilderHttps.create(DownLoadService::class.java)
.downloadFile("bytes=$currentLength-", url)
} else {
retrofitBuilder.create(DownLoadService::class.java)
.downloadFile("bytes=$currentLength-", url)
}
// val response = retrofitBuilder.create(DownLoadService::class.java)
// .downloadFile("bytes=$currentLength-", url)
val responseBody = response.body()
if (responseBody == null) {
"responseBody is null".logi()
Expand Down

0 comments on commit 5c9c3b5

Please sign in to comment.