Skip to content

Commit bd83c41

Browse files
refactor(lib): 重构 HTTP 请求逻辑
- 优化了 HttpRequestViewModel 中的 getNewsByPage 方法,增加了上下文参数 - 重新定义了响应头的解析逻辑,提高了代码可读性和可维护性 - 在 RetrofitFactoryFragment 中更新了对 HttpRequestViewModel 的调用方式 - 引入了 Toast消息显示,提高了用户交互体验
1 parent a60781f commit bd83c41

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

app/src/main/java/com/pengxh/kt/lib/fragments/utils/RetrofitFactoryFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class RetrofitFactoryFragment : KotlinBaseFragment<FragmentUtilsRetrofitFactoryB
2727

2828
override fun initOnCreate(savedInstanceState: Bundle?) {
2929
httpRequestViewModel = ViewModelProvider(this)[HttpRequestViewModel::class.java]
30-
httpRequestViewModel.getNewsByPage("头条", 1)
30+
httpRequestViewModel.getNewsByPage(requireContext(), "头条", 1)
3131
httpRequestViewModel.httpRequestResult.observe(this) {
3232
if (it.status == 0) {
3333
binding.textView.text = it.result.list.first().title
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package com.pengxh.kt.lib.vm
22

3+
import android.content.Context
34
import androidx.lifecycle.MutableLiveData
45
import com.google.gson.Gson
5-
import com.google.gson.JsonParser
6+
import com.google.gson.JsonObject
67
import com.google.gson.reflect.TypeToken
78
import com.pengxh.kt.lib.model.NewsListModel
89
import com.pengxh.kt.lib.utils.RetrofitServiceManager
910
import com.pengxh.kt.lite.base.BaseViewModel
1011
import com.pengxh.kt.lite.extensions.launch
12+
import com.pengxh.kt.lite.extensions.show
1113
import com.pengxh.kt.lite.utils.LoadState
1214

1315
class HttpRequestViewModel : BaseViewModel() {
1416

15-
private val kTag = "HttpRequestViewModel"
16-
private val gson = Gson()
17+
val gson by lazy { Gson() }
1718
val httpRequestResult = MutableLiveData<NewsListModel>()
1819

19-
fun getNewsByPage(channel: String, start: Int) = launch({
20+
fun getNewsByPage(context: Context, channel: String, start: Int) = launch({
2021
loadState.value = LoadState.Loading
2122
val response = RetrofitServiceManager.getNewsByPage(channel, start)
22-
when (response.getResponseCode()) {
23+
val header = response.getResponseHeader()
24+
when (header.first) {
2325
0 -> {
2426
httpRequestResult.value = gson.fromJson<NewsListModel>(
2527
response, object : TypeToken<NewsListModel>() {}.type
@@ -29,18 +31,20 @@ class HttpRequestViewModel : BaseViewModel() {
2931

3032
else -> {
3133
loadState.value = LoadState.Fail
34+
header.second.show(context)
3235
}
3336
}
3437
}, {
3538
loadState.value = LoadState.Fail
3639
})
3740

38-
private fun String.getResponseCode(): Int {
41+
private fun String.getResponseHeader(): Pair<Int, String> {
3942
if (this.isBlank()) {
40-
return 404
43+
return Pair(404, "Invalid Response")
4144
}
42-
val element = JsonParser.parseString(this)
43-
val jsonObject = element.asJsonObject
44-
return jsonObject.get("status").asInt
45+
val jsonObject = gson.fromJson(this, JsonObject::class.java)
46+
val code = jsonObject.get("code").asInt
47+
val message = jsonObject.get("message").asString
48+
return Pair(code, message)
4549
}
4650
}

0 commit comments

Comments
 (0)