pull/977/head
gedoor 4 years ago
parent 32d1c079fb
commit 9069cbd357
  1. 14
      app/src/main/java/io/legado/app/help/http/OkHttpHelper.kt
  2. 50
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  3. 9
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  4. 9
      app/src/main/java/io/legado/app/ui/association/ImportReplaceRuleViewModel.kt
  5. 9
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt
  6. 8
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  7. 9
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt
  8. 9
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexViewModel.kt
  9. 9
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfViewModel.kt
  10. 8
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -1,5 +1,7 @@
package io.legado.app.help.http package io.legado.app.help.http
import io.legado.app.constant.AppConst
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.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
@ -92,9 +94,15 @@ fun getProxyClient(proxy: String? = null): OkHttpClient {
return okHttpClient return okHttpClient
} }
fun OkHttpClient.newCall(builder: Request.Builder.() -> Unit): Call { suspend fun OkHttpClient.newCall(builder: Request.Builder.() -> Unit): ResponseBody {
val requestBuilder = Request.Builder().apply(builder) val requestBuilder = Request.Builder()
return this.newCall(requestBuilder.build()) requestBuilder.header(AppConst.UA_NAME, AppConfig.userAgent)
requestBuilder.apply(builder)
val response = this.newCall(requestBuilder.build()).await()
if (!response.isSuccessful) {
throw IOException("服务器没有响应。")
}
return response.body!!
} }
suspend fun Call.await(): Response = suspendCancellableCoroutine { block -> suspend fun Call.await(): Response = suspendCancellableCoroutine { block ->

@ -1,8 +1,8 @@
package io.legado.app.lib.webdav package io.legado.app.lib.webdav
import io.legado.app.help.http.await
import io.legado.app.help.http.newCall import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import okhttp3.* import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.RequestBody.Companion.asRequestBody
@ -58,18 +58,11 @@ class WebDav(urlStr: String) {
* @return 远程文件是否存在 * @return 远程文件是否存在
*/ */
suspend fun indexFileInfo(): Boolean { suspend fun indexFileInfo(): Boolean {
propFindResponse(ArrayList())?.let { response -> propFindResponse(ArrayList())?.let {
if (!response.isSuccessful) { if (it.text().isNotEmpty()) {
this.exists = false
return false
}
response.body?.let {
@Suppress("BlockingMethodInNonBlockingContext")
if (it.string().isNotEmpty()) {
return true return true
} }
} }
}
return false return false
} }
@ -80,19 +73,13 @@ class WebDav(urlStr: String) {
* @return 文件列表 * @return 文件列表
*/ */
suspend fun listFiles(propsList: ArrayList<String> = ArrayList()): List<WebDav> { suspend fun listFiles(propsList: ArrayList<String> = ArrayList()): List<WebDav> {
propFindResponse(propsList)?.let { response -> propFindResponse(propsList)?.text()?.let { body ->
if (response.isSuccessful) { return parseDir(body)
response.body?.let { body ->
@Suppress("BlockingMethodInNonBlockingContext")
return parseDir(body.string())
}
}
} }
return ArrayList() return ArrayList()
} }
@Throws(IOException::class) private suspend fun propFindResponse(propsList: ArrayList<String>): ResponseBody? {
private suspend fun propFindResponse(propsList: ArrayList<String>): Response? {
val requestProps = StringBuilder() val requestProps = StringBuilder()
for (p in propsList) { for (p in propsList) {
requestProps.append("<a:").append(p).append("/>\n") requestProps.append("<a:").append(p).append("/>\n")
@ -105,14 +92,18 @@ class WebDav(urlStr: String) {
val url = httpUrl val url = httpUrl
val auth = HttpAuth.auth val auth = HttpAuth.auth
if (url != null && auth != null) { if (url != null && auth != null) {
return okHttpClient.newCall { return kotlin.runCatching {
okHttpClient.newCall {
url(url) url(url)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass)) addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
// 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 // 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性
// 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 // 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。
val requestBody = requestPropsStr.toRequestBody("text/plain".toMediaType()) val requestBody = requestPropsStr.toRequestBody("text/plain".toMediaType())
method("PROPFIND", requestBody) method("PROPFIND", requestBody)
}.await() }
}.onFailure {
it.printStackTrace()
}.getOrNull()
} }
return null return null
} }
@ -160,11 +151,12 @@ class WebDav(urlStr: String) {
val url = httpUrl val url = httpUrl
val auth = HttpAuth.auth val auth = HttpAuth.auth
if (url != null && auth != null) { if (url != null && auth != null) {
return okHttpClient.newCall { okHttpClient.newCall {
url(url) url(url)
method("MKCOL", null) method("MKCOL", null)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass)) addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
}.await().isSuccessful }
return true
} }
return false return false
} }
@ -201,11 +193,12 @@ class WebDav(urlStr: String) {
val url = httpUrl val url = httpUrl
val auth = HttpAuth.auth val auth = HttpAuth.auth
if (url != null && auth != null) { if (url != null && auth != null) {
return okHttpClient.newCall { okHttpClient.newCall {
url(url) url(url)
put(fileBody) put(fileBody)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass)) addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
}.await().isSuccessful }
return true
} }
return false return false
} }
@ -216,11 +209,12 @@ class WebDav(urlStr: String) {
val url = httpUrl val url = httpUrl
val auth = HttpAuth.auth val auth = HttpAuth.auth
if (url != null && auth != null) { if (url != null && auth != null) {
return okHttpClient.newCall { okHttpClient.newCall {
url(url) url(url)
put(fileBody) put(fileBody)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass)) addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
}.await().isSuccessful }
return true
} }
return false return false
} }
@ -233,7 +227,7 @@ class WebDav(urlStr: String) {
return okHttpClient.newCall { return okHttpClient.newCall {
url(url) url(url)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass)) addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
}.await().body?.byteStream() }.byteStream()
} }
return null return null
} }

@ -9,13 +9,14 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.SourceHelp import io.legado.app.help.SourceHelp
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.help.storage.OldRule import io.legado.app.help.storage.OldRule
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonArray import io.legado.app.utils.isJsonArray
import io.legado.app.utils.isJsonObject import io.legado.app.utils.isJsonObject
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
var groupName: String? = null var groupName: String? = null
@ -111,7 +112,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
} }
private suspend fun importSourceUrl(url: String) { private suspend fun importSourceUrl(url: String) {
RxHttp.get(url).toText("utf-8").await().let { body -> okHttpClient.newCall {
url(url)
}.text("utf-8").let { body ->
val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$") val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = Restore.jsonPath.parse(item)

@ -4,10 +4,11 @@ import android.app.Application
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.help.storage.OldReplace import io.legado.app.help.storage.OldReplace
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) { class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
val errorLiveData = MutableLiveData<String>() val errorLiveData = MutableLiveData<String>()
@ -18,7 +19,9 @@ class ImportReplaceRuleViewModel(app: Application) : BaseViewModel(app) {
fun import(text: String) { fun import(text: String) {
execute { execute {
if (text.isAbsUrl()) { if (text.isAbsUrl()) {
RxHttp.get(text).toText("utf-8").await().let { okHttpClient.newCall {
url(text)
}.text("utf-8").let {
val rules = OldReplace.jsonToReplaceRules(it) val rules = OldReplace.jsonToReplaceRules(it)
allRules.addAll(rules) allRules.addAll(rules)
} }

@ -9,10 +9,11 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.SourceHelp import io.legado.app.help.SourceHelp
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
import io.legado.app.utils.* import io.legado.app.utils.*
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
var groupName: String? = null var groupName: String? = null
@ -106,7 +107,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
} }
private suspend fun importSourceUrl(url: String) { private suspend fun importSourceUrl(url: String) {
RxHttp.get(url).toText("utf-8").await().let { body -> okHttpClient.newCall {
url(url)
}.text("utf-8").let { body ->
val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$") val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = Restore.jsonPath.parse(item)

@ -17,6 +17,8 @@ import io.legado.app.databinding.DialogReadBgTextBinding
import io.legado.app.databinding.ItemBgImageBinding import io.legado.app.databinding.ItemBgImageBinding
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.permission.Permissions import io.legado.app.lib.permission.Permissions
@ -29,8 +31,6 @@ import io.legado.app.ui.document.FilePicker
import io.legado.app.ui.document.FilePickerParam import io.legado.app.ui.document.FilePickerParam
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toByteArray
import java.io.File import java.io.File
class BgTextConfigDialog : BaseDialogFragment() { class BgTextConfigDialog : BaseDialogFragment() {
@ -308,7 +308,9 @@ class BgTextConfigDialog : BaseDialogFragment() {
private fun importNetConfig(url: String) { private fun importNetConfig(url: String) {
execute { execute {
RxHttp.get(url).toByteArray().await().let { okHttpClient.newCall {
url(url)
}.bytes().let {
importConfig(it) importConfig(it)
} }
}.onError { }.onError {

@ -6,9 +6,10 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.HttpTTS import io.legado.app.data.entities.HttpTTS
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.* import io.legado.app.utils.*
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
class SpeakEngineViewModel(application: Application) : BaseViewModel(application) { class SpeakEngineViewModel(application: Application) : BaseViewModel(application) {
@ -20,7 +21,9 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application
fun importOnLine(url: String) { fun importOnLine(url: String) {
execute { execute {
RxHttp.get(url).toText("utf-8").await().let { json -> okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
import(json) import(json)
} }
}.onSuccess { }.onSuccess {

@ -5,10 +5,11 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.TxtTocRule import io.legado.app.data.entities.TxtTocRule
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
class TocRegexViewModel(application: Application) : BaseViewModel(application) { class TocRegexViewModel(application: Application) : BaseViewModel(application) {
@ -29,7 +30,9 @@ class TocRegexViewModel(application: Application) : BaseViewModel(application) {
fun importOnLine(url: String, finally: (msg: String) -> Unit) { fun importOnLine(url: String, finally: (msg: String) -> Unit) {
execute { execute {
RxHttp.get(url).toText("utf-8").await().let { json -> okHttpClient.newCall {
url(url)
}.text("utf-8").let { json ->
GSON.fromJsonArray<TxtTocRule>(json)?.let { GSON.fromJsonArray<TxtTocRule>(json)?.let {
appDb.txtTocRuleDao.insert(*it.toTypedArray()) appDb.txtTocRuleDao.insert(*it.toTypedArray())
} }

@ -7,13 +7,14 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup import io.legado.app.data.entities.BookGroup
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.model.webBook.PreciseSearch import io.legado.app.model.webBook.PreciseSearch
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toText
class BookshelfViewModel(application: Application) : BaseViewModel(application) { class BookshelfViewModel(application: Application) : BaseViewModel(application) {
@ -87,7 +88,9 @@ class BookshelfViewModel(application: Application) : BaseViewModel(application)
val text = str.trim() val text = str.trim()
when { when {
text.isAbsUrl() -> { text.isAbsUrl() -> {
RxHttp.get(text).toText().await().let { okHttpClient.newCall {
url(text)
}.text().let {
importBookshelf(it, groupId) importBookshelf(it, groupId)
} }
} }

@ -16,6 +16,8 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.data.entities.RssStar import io.legado.app.data.entities.RssStar
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.rss.Rss import io.legado.app.model.rss.Rss
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
@ -24,8 +26,6 @@ import io.legado.app.utils.isContentScheme
import io.legado.app.utils.writeBytes import io.legado.app.utils.writeBytes
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toByteArray
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -147,7 +147,9 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
private suspend fun webData2bitmap(data: String): ByteArray? { private suspend fun webData2bitmap(data: String): ByteArray? {
return if (URLUtil.isValidUrl(data)) { return if (URLUtil.isValidUrl(data)) {
RxHttp.get(data).toByteArray().await() okHttpClient.newCall {
url(data)
}.bytes()
} else { } else {
Base64.decode(data.split(",").toTypedArray()[1], Base64.DEFAULT) Base64.decode(data.split(",").toTypedArray()[1], Base64.DEFAULT)
} }

Loading…
Cancel
Save