pull/1319/head
gedoor 3 years ago
parent 9f92344b97
commit a950f9ec3d
  1. 18
      app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
  2. 4
      app/src/main/java/io/legado/app/model/CacheBook.kt

@ -4,7 +4,9 @@ import io.legado.app.constant.AppConst
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.utils.EncodingDetect import io.legado.app.utils.EncodingDetect
import io.legado.app.utils.UTF8BOMFighter import io.legado.app.utils.UTF8BOMFighter
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import okhttp3.* import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
@ -18,34 +20,38 @@ suspend fun OkHttpClient.newCall(
retry: Int = 0, retry: Int = 0,
builder: Request.Builder.() -> Unit builder: Request.Builder.() -> Unit
): ResponseBody { ): ResponseBody {
return withContext(IO) {
val requestBuilder = Request.Builder() val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder) requestBuilder.apply(builder)
var response: Response? = null var response: Response? = null
for (i in 0..retry) { for (i in 0..retry) {
response = this.newCall(requestBuilder.build()).await() response = this@newCall.newCall(requestBuilder.build()).await()
if (response.isSuccessful) { if (response.isSuccessful) {
return response.body!! return@withContext response.body!!
} }
} }
return response!!.body ?: throw IOException(response.message) return@withContext response!!.body ?: throw IOException(response.message)
}
} }
suspend fun OkHttpClient.newCallStrResponse( suspend fun OkHttpClient.newCallStrResponse(
retry: Int = 0, retry: Int = 0,
builder: Request.Builder.() -> Unit builder: Request.Builder.() -> Unit
): StrResponse { ): StrResponse {
return withContext(IO) {
val requestBuilder = Request.Builder() val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent) requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder) requestBuilder.apply(builder)
var response: Response? = null var response: Response? = null
for (i in 0..retry) { for (i in 0..retry) {
response = this.newCall(requestBuilder.build()).await() response = this@newCallStrResponse.newCall(requestBuilder.build()).await()
if (response.isSuccessful) { if (response.isSuccessful) {
return StrResponse(response, response.body!!.text()) return@withContext StrResponse(response, response.body!!.text())
}
} }
return@withContext StrResponse(response!!, response.body?.text() ?: response.message)
} }
return StrResponse(response!!, response.body?.text() ?: response.message)
} }
suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->

@ -70,10 +70,10 @@ class CacheBook(var bookSource: BookSource, var book: Book) {
} }
} }
fun start(context: Context, bookUrl: String, start: Int, end: Int) { fun start(context: Context, book: Book, start: Int, end: Int) {
context.startService<CacheBookService> { context.startService<CacheBookService> {
action = IntentAction.start action = IntentAction.start
putExtra("bookUrl", bookUrl) putExtra("bookUrl", book.bookUrl)
putExtra("start", start) putExtra("start", start)
putExtra("end", end) putExtra("end", end)
} }

Loading…
Cancel
Save