Skip to content

Commit cd68a50

Browse files
committed
v2.5.5
优化 1. 错误打印
1 parent 159e2ad commit cd68a50

File tree

10 files changed

+136
-108
lines changed

10 files changed

+136
-108
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
minSdkVersion 23
1313
//noinspection OldTargetApi
1414
targetSdkVersion 34
15-
versionCode 77
16-
versionName '2.5.4'
15+
versionCode 78
16+
versionName '2.5.5'
1717
resourceConfigurations += ['zh', 'zh-rCN']
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

app/src/main/java/top/fumiama/copymanga/LoginActivity.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ class LoginActivity : AppCompatActivity() {
2626
alblogin.setOnClickListener {
2727
lifecycleScope.launch {
2828
val salt = Random.nextInt(10000)
29-
val username = altusrnm.text?.toString() ?: run {
29+
val username = altusrnm.text?.toString()
30+
if (username.isNullOrEmpty()) {
3031
Toast.makeText(
3132
this@LoginActivity,
3233
R.string.login_null_username,
3334
Toast.LENGTH_SHORT
3435
).show()
3536
return@launch
3637
}
37-
val pwd = altpwd.text?.toString() ?: run {
38+
val pwd = altpwd.text?.toString()
39+
if (pwd.isNullOrEmpty()) {
3840
Toast.makeText(this@LoginActivity, R.string.login_null_pwd, Toast.LENGTH_SHORT)
3941
.show()
4042
return@launch
@@ -50,14 +52,23 @@ class LoginActivity : AppCompatActivity() {
5052
finish()
5153
return@launch
5254
}
53-
val l = MainActivity.member?.login(username, pwd, salt)
54-
Log.d("MyLA", "login return code: ${l?.code}")
55-
if (l?.code == 200) {
56-
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
57-
finish()
58-
return@launch
55+
try {
56+
val l = MainActivity.member?.login(username, pwd, salt)
57+
Log.d("MyLA", "login return code: ${l?.code}")
58+
if (l?.code == 200) {
59+
MainActivity.mainWeakReference?.get()?.refreshUserInfo()
60+
finish()
61+
return@launch
62+
}
63+
Toast.makeText(this@LoginActivity, "错误码${l?.code}: ${l?.message}", Toast.LENGTH_LONG).show()
64+
} catch (e: Exception) {
65+
e.printStackTrace()
66+
Toast.makeText(
67+
this@LoginActivity,
68+
"${e::class.simpleName} ${e.message}",
69+
Toast.LENGTH_SHORT
70+
).show()
5971
}
60-
Toast.makeText(this@LoginActivity, l?.message, Toast.LENGTH_LONG).show()
6172
}
6273
}
6374
if (Config.general_enable_transparent_system_bar.value) {

app/src/main/java/top/fumiama/copymanga/api/manga/Book.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ class Book(val path: String, private val getString: (Int) -> String, private val
8484
} catch (e: Exception) {
8585
e.printStackTrace()
8686
MainActivity.mainWeakReference?.get()?.apply {
87-
runOnUiThread { Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show() }
87+
withContext(Dispatchers.Main) {
88+
runOnUiThread { Toast.makeText(this@apply, "${e::class.simpleName} ${e.message}", Toast.LENGTH_SHORT).show() }
89+
}
8890
}
8991
}
9092
}

app/src/main/java/top/fumiama/copymanga/api/manga/Shelf.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Shelf(private val getString: (Int) -> String) {
1515
private val delApiUrl get() = "${apiUrl}s?platform=${Config.platform.value}"
1616
suspend fun add(comicId: String): String = withContext(Dispatchers.IO) {
1717
if (comicId.isEmpty()) {
18-
return@withContext "空漫画ID"
18+
throw IllegalArgumentException("空漫画ID")
1919
}
2020
val body = buildString {
2121
append("comic_id=")
@@ -24,19 +24,15 @@ class Shelf(private val getString: (Int) -> String) {
2424
append("")
2525
append(Config.token.value)
2626
}
27-
return@withContext try {
28-
val re = Config.myHostApiUrl.request(
29-
addApiUrl, body.encodeToByteArray(), "POST",
30-
"application/x-www-form-urlencoded;charset=utf-8")
31-
Gson().fromJson(re, ReturnBase::class.java).message
32-
} catch (e: Exception) {
33-
e.message?:e::class.simpleName?:e.toString()
34-
}
27+
val re = Config.myHostApiUrl.request(
28+
addApiUrl, body.encodeToByteArray(), "POST",
29+
"application/x-www-form-urlencoded;charset=utf-8")
30+
Gson().fromJson(re, ReturnBase::class.java).message
3531
}
3632

3733
suspend fun del(vararg bookIds: Int): String = withContext(Dispatchers.IO) {
3834
if (bookIds.isEmpty()) {
39-
return@withContext "空ID列表"
35+
throw IllegalArgumentException("空ID列表")
4036
}
4137
val body = buildString {
4238
bookIds.forEach {
@@ -47,14 +43,10 @@ class Shelf(private val getString: (Int) -> String) {
4743
append("authorization=Token+")
4844
append(Config.token.value)
4945
}
50-
return@withContext try {
51-
val re = Config.myHostApiUrl.request(
52-
delApiUrl, body.encodeToByteArray(),
53-
"DELETE", "application/x-www-form-urlencoded;charset=utf-8")
54-
Gson().fromJson(re, ReturnBase::class.java).message
55-
} catch (e: Exception) {
56-
e.message?:e::class.simpleName?:e.toString()
57-
}
46+
val re = Config.myHostApiUrl.request(
47+
delApiUrl, body.encodeToByteArray(),
48+
"DELETE", "application/x-www-form-urlencoded;charset=utf-8")
49+
Gson().fromJson(re, ReturnBase::class.java).message
5850
}
5951

6052
suspend fun query(pathWord: String): BookQueryStructure? = withContext(Dispatchers.IO) {

app/src/main/java/top/fumiama/copymanga/api/network/Api.kt

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package top.fumiama.copymanga.api.network
33
import android.util.Log
44
import android.widget.Toast
55
import com.google.gson.Gson
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.sync.Mutex
8+
import kotlinx.coroutines.sync.withLock
9+
import kotlinx.coroutines.withContext
610
import top.fumiama.copymanga.MainActivity
711
import top.fumiama.copymanga.api.Config.apiProxy
812
import top.fumiama.copymanga.api.Config.networkApiUrl
@@ -13,13 +17,10 @@ import top.fumiama.copymanga.json.NetworkStructure
1317
import top.fumiama.copymanga.json.ReturnBase
1418
import top.fumiama.copymanga.net.DownloadTools
1519
import top.fumiama.dmzj.copymanga.R
16-
import java.util.concurrent.locks.ReentrantReadWriteLock
17-
import kotlin.concurrent.read
18-
import kotlin.concurrent.write
1920

2021
class Api {
2122
private var mHostApiUrls = mutableListOf<String>()
22-
private var mu = ReentrantReadWriteLock()
23+
private var mu = Mutex()
2324

2425
fun getApis(): Array<String> {
2526
return mHostApiUrls.toTypedArray()
@@ -28,12 +29,12 @@ class Api {
2829
suspend fun init() {
2930
if (mHostApiUrls.isNotEmpty()) return
3031
if (reverseProxyUrl.value.isNotEmpty() && reverseProxyUrl.value != proxyUrl) {
31-
mu.write { mHostApiUrls = mutableListOf(reverseProxyUrl.value) }
32+
mu.withLock { mHostApiUrls = mutableListOf(reverseProxyUrl.value) }
3233
Log.d("MyApi", "myHostApiUrl set reverse proxy to ${reverseProxyUrl.value}")
3334
return
3435
}
3536
MainActivity.mainWeakReference?.get()?.apply {
36-
mu.write {
37+
mu.withLock {
3738
if (mHostApiUrls.isNotEmpty()) return
3839
try {
3940
val d = get(getString(R.string.networkApiUrl).format(platform.value), networkApiUrl.value)
@@ -48,15 +49,19 @@ class Api {
4849
} catch (e: Exception) {
4950
e.printStackTrace()
5051
mHostApiUrls = mutableListOf(networkApiUrl.value)
51-
runOnUiThread {
52-
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
52+
withContext(Dispatchers.Main) {
53+
runOnUiThread {
54+
Toast.makeText(this@apply, "${e::class.simpleName} ${e.message}", Toast.LENGTH_SHORT).show()
55+
}
5356
}
5457
}
5558
if (mHostApiUrls.isEmpty()) {
5659
mHostApiUrls = mutableListOf(networkApiUrl.value)
5760
Log.d("MyApi", "myHostApiUrl set default ${mHostApiUrls[0]}")
58-
runOnUiThread {
59-
Toast.makeText(this, "无法获取API列表", Toast.LENGTH_SHORT).show()
61+
withContext(Dispatchers.Main) {
62+
runOnUiThread {
63+
Toast.makeText(this@apply, "无法获取API列表", Toast.LENGTH_SHORT).show()
64+
}
6065
}
6166
}
6267
}
@@ -65,7 +70,7 @@ class Api {
6570
}
6671
// get throw error on non-json or non-200 or empty apis, path: /api/v3/xxx, return json string
6772
suspend fun get(path: String, forceApi: String? = null): String {
68-
val apis = if (forceApi == null) mu.read { mHostApiUrls.toTypedArray() } else arrayOf(forceApi)
73+
val apis = if (forceApi == null) mu.withLock { mHostApiUrls.toTypedArray() } else arrayOf(forceApi)
6974
if (apis.isEmpty()) {
7075
throw NoSuchElementException("API列表为空")
7176
}
@@ -78,13 +83,22 @@ class Api {
7883
}?: DownloadTools.getApiContent(u)).decodeToString()
7984
r = Gson().fromJson(ret, ReturnBase::class.java)
8085
if (r!!.code != 200) {
81-
mu.write { mHostApiUrls.remove(api) }
86+
withContext(Dispatchers.Main) {
87+
MainActivity.mainWeakReference?.get()?.apply {
88+
runOnUiThread {
89+
Toast.makeText(this, "错误码${r?.code?:-1}, 信息: ${r?.message?:""}", Toast.LENGTH_SHORT).show()
90+
}
91+
}
92+
}
8293
} else {
8394
return ret
8495
}
8596
} catch (e: Exception) {
86-
mu.write { mHostApiUrls.remove(api) }
87-
if (i >= apis.size-1) { // throw lase exception
97+
mu.withLock {
98+
if (mHostApiUrls.size <= 1) return@withLock
99+
mHostApiUrls.remove(api)
100+
}
101+
if (i >= apis.size-1) { // throw last exception
88102
throw e
89103
}
90104
}
@@ -93,34 +107,40 @@ class Api {
93107
}
94108
// request throw error on non-json or non-200 or empty apis, path: /api/v3/xxx, return json string
95109
suspend fun request(path: String, body: ByteArray, method: String, contentType: String, forceApi: String? = null): String {
96-
val apis = if (forceApi == null) mu.read { mHostApiUrls } else mutableListOf(forceApi)
110+
val apis = if (forceApi == null) mu.withLock { mHostApiUrls } else mutableListOf(forceApi)
97111
if (apis.isEmpty()) {
98112
throw NoSuchElementException("API列表为空")
99113
}
100114
var r: ReturnBase? = null
101-
apis.forEach { api ->
115+
apis.forEachIndexed { i, api ->
102116
val u = "https://$api$path"
103117
try {
104118
val ret = (apiProxy?.comancry(u) {
105119
DownloadTools.requestApiWithBody(u, method, body, contentType)
106120
}?: DownloadTools.requestApiWithBody(u, method, body, contentType)).decodeToString()
107121
r = Gson().fromJson(ret, ReturnBase::class.java)
108122
if (r!!.code != 200) {
109-
mu.write {
110-
if (mHostApiUrls.size <= 1) return@write
111-
mHostApiUrls.remove(api)
123+
withContext(Dispatchers.Main) {
124+
MainActivity.mainWeakReference?.get()?.apply {
125+
runOnUiThread {
126+
Toast.makeText(this, "错误码${r?.code?:-1}, 信息: ${r?.message?:""}", Toast.LENGTH_SHORT).show()
127+
}
128+
}
112129
}
113130
} else {
114131
return ret
115132
}
116133
} catch (e: Exception) {
117134
e.printStackTrace()
118-
mu.write {
119-
if (mHostApiUrls.size <= 1) return@write
135+
mu.withLock {
136+
if (mHostApiUrls.size <= 1) return@withLock
120137
mHostApiUrls.remove(api)
121138
}
139+
if (i >= apis.size-1) { // throw last exception
140+
throw e
141+
}
122142
}
123143
}
124-
throw IllegalStateException("错误码${r!!.code}, 信息: ${r!!.message}")
144+
throw IllegalStateException("错误码${r?.code?:-1}, 信息: ${r?.message?:""}")
125145
}
126146
}

app/src/main/java/top/fumiama/copymanga/api/update/Update.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object Update {
7070
Toast.makeText(this@apply, "下载成功", Toast.LENGTH_SHORT).show()
7171
info.dismiss()
7272
install(it, this@apply)
73-
} else runOnUiThread {
73+
} else {
7474
Toast.makeText(this@apply, "文件损坏", Toast.LENGTH_SHORT).show()
7575
info.dismiss()
7676
}

app/src/main/java/top/fumiama/copymanga/api/user/Member.kt

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package top.fumiama.copymanga.api.user
22

33
import android.util.Base64
44
import com.google.gson.Gson
5+
import com.google.gson.JsonSyntaxException
56
import kotlinx.coroutines.Dispatchers
67
import kotlinx.coroutines.withContext
78
import top.fumiama.copymanga.api.Config
@@ -14,14 +15,7 @@ class Member(private val getString: (Int) -> String) {
1415
val hasLogin: Boolean get() = Config.token.value?.isNotEmpty() ?: false
1516
suspend fun login(username: String, pwd: String, salt: Int): LoginInfoStructure =
1617
withContext(Dispatchers.IO) {
17-
return@withContext try {
18-
saveInfo(postLogin(username, pwd, salt))
19-
} catch (e: Exception) {
20-
val l = LoginInfoStructure()
21-
l.code = 400
22-
l.message = e.message.toString()
23-
l
24-
}
18+
return@withContext saveInfo(postLogin(username, pwd, salt))
2519
}
2620

2721
/**
@@ -32,30 +26,13 @@ class Member(private val getString: (Int) -> String) {
3226
*/
3327
suspend fun info(): LoginInfoStructure = withContext(Dispatchers.IO) {
3428
if (!hasLogin) {
35-
val l = LoginInfoStructure()
36-
l.code = 449
37-
l.message = getString(R.string.noLogin)
38-
return@withContext l
39-
}
40-
try {
41-
val u = getString(R.string.memberInfoApiUrl)
42-
.format(Config.platform.value)
43-
try {
44-
val l = Gson().fromJson(Config.myHostApiUrl.get(u), LoginInfoStructure::class.java)
45-
if (l.code == 200) Config.avatar.value = l.results.avatar
46-
l
47-
} catch (e: Exception) {
48-
val l = LoginInfoStructure()
49-
l.code = 450
50-
l.message = "${getString(R.string.login_get_avatar_failed)}: ${e.message}"
51-
l
52-
}
53-
} catch (e: Exception) {
54-
val l = LoginInfoStructure()
55-
l.code = 450
56-
l.message = "${getString(R.string.login_get_avatar_failed)}: ${e.message}"
57-
l
29+
throw IllegalArgumentException(getString(R.string.noLogin))
5830
}
31+
val u = getString(R.string.memberInfoApiUrl)
32+
.format(Config.platform.value)
33+
val l = Gson().fromJson(Config.myHostApiUrl.get(u), LoginInfoStructure::class.java)
34+
if (l.code == 200) Config.avatar.value = l.results.avatar
35+
l
5936
}
6037

6138
suspend fun logout() = withContext(Dispatchers.IO) {
@@ -78,8 +55,8 @@ class Member(private val getString: (Int) -> String) {
7855
}
7956
return@use l
8057
} ?: throw Exception(getString(R.string.login_parse_json_error))
81-
} catch (e: Exception) {
82-
throw Exception(data.decodeToString(), e)
58+
} catch (e: JsonSyntaxException) {
59+
throw JsonSyntaxException(data.decodeToString(), e)
8360
}
8461
}
8562

app/src/main/java/top/fumiama/copymanga/net/DownloadTools.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ object DownloadTools {
207207
Log.d("MyDT", "getHttpContent: ${ret.size} bytes")
208208
if (!u.startsWith("https://$proxyUrl") &&
209209
conn.getHeaderField("Content-type") != "application/json") {
210-
throw IllegalStateException("请求错误: ${ret.decodeToString()}")
210+
throw IllegalStateException("非JSON返回: ${ret.decodeToString()}")
211211
}
212212
decodeBody(ret, conn.getHeaderField("Content-Encoding")?:"")
213213
}

0 commit comments

Comments
 (0)