Giter Site home page Giter Site logo

Comments (6)

Quyunshuo avatar Quyunshuo commented on July 19, 2024

能上段代码吗?

from androidbaseframemvvm.

chenmingyin avatar chenmingyin commented on July 19, 2024

ApiService接口里这样
@POST("user") suspend fun <T> getUserManage( @Body result: RequestBody ): ResponseResult<T>

ResponseResult是返回结果的数据类
data class ResponseResult<T> ( val resultCode: Int, val message: String, val result: T )
因为返回数据里只有result字段里的数据不同,我就想把它弄成一个泛型,结果默认的gson的ConverterFactory可能解析有问题,不能把泛型解析成javaBean。
Retrofit.Builder() .client(okHttpClient) .baseUrl(NetworkUrl.BASE_HOST) .addConverterFactory(GsonConverterFactory.create()) .build()
我现在看到网上好像说要自定义GsonConverterFactory,想问大佬有没有什么解决办法

from androidbaseframemvvm.

Quyunshuo avatar Quyunshuo commented on July 19, 2024

你应该是想把公共部分抽离出来,我可以给你看下我是怎么写的,你看下是否可以解决你的问题
我写了一个返回数据的基类,和你结构一致是{"code":"", "msg":"","data": T}这种类型的

data class BaseResponse<D>(
    val code: String,
    val `data`: D,
    val msg: String
)

这个数据类的data是泛型,在使用时是这样的 比如我有一个{"code":"200", "msg":"xxx","data": ["a","b"]}这种格式的数据,在ApiService接口可以这样定义

@GET("padVersion?")
suspend fun getTestData(@Query("version") versionCode: Int): BaseResponse<List<String>>

我在项目中是这么写的

from androidbaseframemvvm.

chenmingyin avatar chenmingyin commented on July 19, 2024

我的项目里是共用一个接口,通过传入不同的请求参数获得不同的返回数据,所以我在ApiService里面只写了一个函数。
suspend fun <T> getUserInfo( @Body result: RequestBody ): ResponseResult<T>
当在Repositiory中调用时,如这样调用时NetworkRequest.userManageService.getUserInfo<UserBean>(),返回值ResponseResult里好像获取不到UserBean,我刚学kotlin没多久,不知道是不是我的泛型函数写的有问题。
像你上面这种写法可以正确运行,但是对我这个只有一个接口的项目来说,需要写很多函数才行。
我现在不知道这个泛型函数应该怎么写?求大佬指点!

from androidbaseframemvvm.

Quyunshuo avatar Quyunshuo commented on July 19, 2024

我没有办法,似乎你的思路行不通,你以前用Java的时候,这种方式不会异常吗

from androidbaseframemvvm.

chenmingyin avatar chenmingyin commented on July 19, 2024

好像是的,我在网上查到好像是通过内联函数来做的,看来暂时只能通过写多个函数来解决了。
谢谢你的回复。

from androidbaseframemvvm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.