pull/1319/head
gedoor 3 years ago
parent 9f92344b97
commit a950f9ec3d
  1. 42
      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.utils.EncodingDetect
import io.legado.app.utils.UTF8BOMFighter
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType.Companion.toMediaType
@ -18,34 +20,38 @@ suspend fun OkHttpClient.newCall(
retry: Int = 0,
builder: Request.Builder.() -> Unit
): ResponseBody {
val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
response = this.newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
return response.body!!
return withContext(IO) {
val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
response = this@newCall.newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
return@withContext response.body!!
}
}
return@withContext response!!.body ?: throw IOException(response.message)
}
return response!!.body ?: throw IOException(response.message)
}
suspend fun OkHttpClient.newCallStrResponse(
retry: Int = 0,
builder: Request.Builder.() -> Unit
): StrResponse {
val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
response = this.newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
return StrResponse(response, response.body!!.text())
return withContext(IO) {
val requestBuilder = Request.Builder()
requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder)
var response: Response? = null
for (i in 0..retry) {
response = this@newCallStrResponse.newCall(requestBuilder.build()).await()
if (response.isSuccessful) {
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 ->

@ -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> {
action = IntentAction.start
putExtra("bookUrl", bookUrl)
putExtra("bookUrl", book.bookUrl)
putExtra("start", start)
putExtra("end", end)
}

Loading…
Cancel
Save