refactor(network): 优化网络请求初始化逻辑并更新服务器配置
- 移除 README.md 文件 - 在 `MyApplication` 中将 `baseUrl` 更新为测试服务器地址 `http://192.168.27.248:8080` - 重构 `WelcomeVM` 中的网络请求逻辑,移除冗余的 `demoComponent` 和 `service` 变量 - 统一使用 `HttpManager.getApi(Service::class.java)` 直接获取接口实例 - 简化 `WelcomeVM` 结构,移除硬编码的局域网 IP 常量
这个提交包含在:
父节点
b640a7e7e5
当前提交
16378820c8
@ -13,7 +13,7 @@ import com.xuqm.base.di.manager.HttpManager;
|
||||
*/
|
||||
public class MyApplication extends App {
|
||||
|
||||
public static String baseUrl = "http://10.10.203.120:31734";
|
||||
public static String baseUrl = "http://192.168.27.248:8080";
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
|
||||
@ -2,6 +2,7 @@ package com.nova.brain.glass.viewmodel
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.nova.brain.glass.repository.Service
|
||||
import com.xuqm.base.App
|
||||
import com.xuqm.base.di.manager.HttpManager
|
||||
import com.xuqm.sdhbwfu.core.viewModel.BaseViewModel
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
@ -11,23 +12,9 @@ import okhttp3.RequestBody
|
||||
class WelcomeVM : BaseViewModel() {
|
||||
|
||||
val result = MutableLiveData<String>()
|
||||
|
||||
companion object {
|
||||
// 修改为运行 server/app.py 的机器在局域网中的 IP
|
||||
const val DEMO_SERVER_URL = "http://192.168.27.248:8080/"
|
||||
}
|
||||
|
||||
private val demoComponent by lazy {
|
||||
HttpManager.getAppComponent(DEMO_SERVER_URL)
|
||||
}
|
||||
|
||||
private val service by lazy {
|
||||
HttpManager.getApi(demoComponent, Service::class.java)
|
||||
}
|
||||
|
||||
fun demoGet() {
|
||||
result.value = "GET 请求中..."
|
||||
service.demoGet()
|
||||
HttpManager.getApi(Service::class.java).demoGet()
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe({ body ->
|
||||
result.postValue("GET 响应:\n${body.string()}")
|
||||
@ -40,7 +27,7 @@ class WelcomeVM : BaseViewModel() {
|
||||
result.value = "POST 请求中..."
|
||||
val json = """{"demo":"post","from":"glass"}"""
|
||||
val body = RequestBody.create(MediaType.parse("application/json"), json)
|
||||
service.demoPost(body)
|
||||
HttpManager.getApi(Service::class.java).demoPost(body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe({ resp ->
|
||||
result.postValue("POST 响应:\n${resp.string()}")
|
||||
@ -51,7 +38,7 @@ class WelcomeVM : BaseViewModel() {
|
||||
|
||||
fun demoPostSse() {
|
||||
result.postValue("SSE 连接中...")
|
||||
service.demoStream(5)
|
||||
HttpManager.getApi(Service::class.java).demoStream(5)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.subscribe({ body ->
|
||||
val sb = StringBuilder("SSE 流式响应:\n")
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户