-2

我正在尝试使用改造向远程服务器发出请求。我正在使用干净的架构来分离关注点。从顶级文件

改造电话

@GET("/api/dedicated_trips")
    suspend fun getDedicatedTrips(
        @Header("Authorization") token: String,
    ): DedicatedTripDto

专用旅行Dto

data class DedicatedTripDto(
    val `data`: List<Data>,
    val error: Boolean
)

数据


data class Data(
    val depot_location: String,
    val depot_name: String,
    val fuel_amount: Int,
    val fuel_in_litres: Int,
    val fuel_receipt: String?,
    val milleage: Int,
    val no_of_vehicles: Int,
    val odometer_reading: String?,
    val partner_email: String,
    val partner_name: String,
    val pump_reading: String?,
    val registration_number: String,
    val status: String,
    val tracking_no: Int,
    val type: String,
    val vehicle_make: String
)

这是我的用例

class GetDedicatedTripUseCase @Inject constructor(
    private val tripRepository: TripRepository
) {

    suspend operator fun invoke(
        token: String,
    ): Flow<Resource<DedicatedTripDto>> = flow {
        try {
            emit(Resource.Loading())
            val response = tripRepository.getDedicatedTrips(token)
            emit(Resource.Success(response))
        } catch (e: HttpException) {
            emit(Resource.Failure(e.localizedMessage ?: "An expected error occurred"))
        } catch (e: IOException) {
            emit(Resource.Failure("Couldn't connect to server, check your internet connection"))
        }
    }

}

资源


sealed class Resource<out T> {
    class Loading<T>: Resource<T>()
    object Empty: Resource<Nothing>()
    data class Success<out T>(val data: T): Resource<T>()
    data class Failure<out T>(val errorMessage: String?): Resource<T>()

    companion object {
        fun <T> loading() = Loading<T>()
        fun <T> success(data: T) = Success(data)
        fun <T> failed(message: String) = Failure<T>(message)
    }
}

在视图模型方面

@HiltViewModel
class TripViewModel @Inject constructor(
    private val getDedicatedTripUseCase: GetDedicatedTripUseCase,
): ViewModel() {

   private var _dedicatedTripsState = MutableStateFlow<Resource<DedicatedTripDto>>(Resource.Empty)
   val dedicatedTripsState get() = _dedicatedTripsState.asStateFlow()

  fun getTrips(
        token: String,
    ) = viewModelScope.launch {
        getDedicatedTripUseCase(token).collect { state->
            when(state) {
                is Resource.Loading -> {
                    _dedicatedTripsState.value = Resource.loading()
                }
                is Resource.Success -> {
                    _dedicatedTripsState.value = Resource.success(state.data)
                }
                is Resource.Failure -> {
                    _dedicatedTripsState.value = Resource.failed(state.errorMessage!!)
                }
            }
        }
    }

}

在视图端这里是如何调用视图模型代码

@AndroidEntryPoint
class TripDetailsFragment : Fragment() {

  private val tripViewModel: TripViewModel by viewModels()
   private var token: String? = null

 override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
       tripViewModel.authToken.observe(viewLifecycleOwner) {
            token = "Bearer $it"
           //Here is where I'm invoking the function from the view model
            tripViewModel.getTrips(token!!)
        }
}
}

当我调用该函数时,应用程序崩溃,而 logcat 中没有显示任何错误。

json文件

{
    "error": false,
    "data": [
        {
            "tracking_no": 18842224,
            "milleage": 56,
            "odometer_reading": "odometer/starting-odometer-18842224.jpg",
            "fuel_amount": 0,
            "fuel_in_litres": 0,
            "pump_reading": null,
            "fuel_receipt": null,
            "type": "starting",
            "status": "closed",
            "no_of_vehicles": 4,
            "depot_name": "MURANGA TOWN (fomer Fort Hall), Kenya",
            "depot_location": "MURANGA TOWN (fomer Fort Hall), Kenya",
            "vehicle_make": "Isuzu",
            "registration_number": "KDP 234A",
            "partner_name": "DHL",
            "partner_email": "info@dafric.net"
        },
        ...
    ]
}


4

1 回答 1

1

您是否在 AndroidManifest 文件中授予了 Internet 权限?

你确定 api 工作正常吗?也许它正在返回错误或其他格式的东西。

尝试逐行放置日志和/或断点以查找问题。

此外,也许有些字段是空的,而您不知道。尝试(暂时)将所有数据类型更改为可为空的字符串(字符串?),看看它是否有所作为。

于 2022-01-14T00:33:45.493 回答