Android Web服务响应的通用方法

时间:2018-11-07 05:00:16

标签: android generics kotlin retrofit android-livedata

  

我正在从Web服务获取响应。除卡车和猫之类的阵列外,所有响应都相同。下面是两个示例,供您理解。我正在使用liveData和改造。.

在AppWebServices界面中,我创建了GsonBuilder并设置了registerTypeAdapter(WebServiceResponse::class.java, Deserializer<WebServiceResponse<Company>>(Company::class.java))

我希望使WebServiceResponse <>中的该公司动态化。我为AppWebServices尝试了T型,但无法正常工作。

{
    "status": true,
    "statusMessage" : "Success",
    "data": {
        "trucks": [{
                "id": 1,
                "engine": "big",
                "wheels" : 12
                }, 
                { 
                "id": 2,
                "engine": "super big",
                "wheels" : 128
                }]
            }
  }

第二个样品

{
        "status": true,
        "statusMessage" : "Success",
        "data": {
            "cats": [{
                    "id": 1,
                    "title": "Cat 1"
                    }, 
                    { 
                    "id": 2,
                    "title": "Cat 2"
                    }]
                }
      }

搜索SO之后,我发现a solution。一切正常。

CompaniesActivity.class

class CompaniesActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_companies)
        initializeUi()
    }

    private fun initializeUi() {
        val factory = InjectorUtils.provideCompanyViewModelFactory()
        val viewModel = ViewModelProviders.of(this, factory)
            .get(CompanyViewModel::class.java)
        viewModel.getCompanies().observe(this, Observer { companies ->
            setupRecyclerView(companies)
        })
    }

    private fun setupRecyclerView(companies: List<Company>) {
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = CompanyAdapter(companies)
    }
}

InjectorUtils.class

object InjectorUtils {

    fun provideCompanyViewModelFactory() : CompanyViewModelFactory {
        val companyDao = Database.getInstance().companyDao
        val companyRepository = CompanyRepository.getInstance(companyDao)
        return CompanyViewModelFactory(companyRepository)
    }
}

CompanyViewModel.class

class CompanyViewModel(private val companyRepository: CompanyRepository) : ViewModel() {

    fun addCompany(company: Company) = companyRepository.addCompany(company)

    fun addcompanies(companies: List<Company>) = companyRepository.addCompanies(companies)

    fun getCompanies() = companyRepository.getCompanies()
}

CompanyRepository.class

class CompanyRepository private constructor(private val companyDao: CompanyDao) {

    companion object {
        @Volatile private var instance: CompanyRepository? = null
        fun getInstance(companyDao: CompanyDao) =
            instance ?: synchronized(this) {
                instance
                    ?: CompanyRepository(companyDao).also { instance = it }
            }
    }

    fun addCompany(company: Company) {
        companyDao.addCompany(company)
    }

    fun addCompanies(companies: List<Company>) {
        companyDao.addCompanies(companies)
    }

    private fun updatedCompanies(): LiveData<List<Company>> {
        return companyDao.getCompanies()
    }

    fun getCompanies(): LiveData<List<Company>> {
        val companies = companyDao.getCompanies().value
        if (companies == null || companies.isEmpty()) {
            val appWebServices = AppWebServices()
            GlobalScope.launch(Dispatchers.Main) {
                val response = appWebServices.getNearestCompanies(33.6658432, 73.0726399, 0, 100).await()
                if (response.status && response.result.isNotEmpty()) {
                    addCompanies(response.result)
                }
            }
        }
        return updatedCompanies()
    }

}

Deserializer.java

class Deserializer<T>(private val clazz: Type): JsonDeserializer<WebServiceResponse<T>> {

    @Throws(JsonParseException::class)
    override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): WebServiceResponse<T>? {
        val jsonObject = json as JsonObject
        var status = true ///jsonObject.get("status")
        var statusMessage = "hello"//jsonObject.get("statusMessage")

        val data = jsonObject.get("data") as JsonObject
        val companies = data.get("companies") as JsonArray

        val list = mutableListOf<T>();
        for (element in companies) {
            val ele = element as JsonElement
            list.add(context!!.deserialize(ele, clazz))
        }

        return WebServiceResponse<T>(list,status,statusMessage);
    }
}

AppWebServices.class 改造接口

interface AppWebServices {

    @GET(Keys.GET_NEARST_COMPANY_LIST)
    fun getNearestCompanies(
        @Query(Keys.CURRENT_LAT) latitude: Double,
        @Query(Keys.CURRENT_LON) longitude: Double,
        @Query(Keys.START_LIST_INDEX) startIndex: Int,
        @Query(Keys.END_LIST_INDEX) endIndex: Int
    ) : Deferred<WebServiceResponse<Company>>

    companion object {
        operator fun invoke(): AppWebServices {
            val okHttpClient = OkHttpClient.Builder().build()

            var gson = GsonBuilder().setLenient()
                .registerTypeAdapter(WebServiceResponse::class.java, Deserializer<WebServiceResponse<Company>>(Company::class.java))
                .create()
            return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BuildConfig.BASE_URL)
                .addCallAdapterFactory(CoroutineCallAdapterFactory())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()
                .create(AppWebServices::class.java)
        }
    }
}

WebServiceResponse.class

data class WebServiceResponse<T> (
    @SerializedName(Keys.DATA)
    val result: List<T>,
    val status: Boolean,
    val statusMessage: String
)

CompanyDao.class

class CompanyDao {
    private val companyList = mutableListOf<Company>()
    private val companies = MutableLiveData<List<Company>>()

    init {
        companies.value = companyList
    }

    fun addCompany(company: Company) {
        companyList.add(company)
        companies.value = companyList
    }

    fun addCompanies(companies: List<Company>) {
        companyList.addAll(companies)
        this.companies.value = companyList
    }

    fun getCompanies() = companies as LiveData<List<Company>>
}

我想要一个解决方案来告诉我的服务我想要什么样的响应
val appWebServices = AppWebServices<Company>(),因此可以将其设置为

.registerTypeAdapter(WebServiceResponse::class.java, Deserializer<WebServiceResponse<T>>(T::class.java))

1 个答案:

答案 0 :(得分:1)

您可以使用inline reified来访问函数体内的泛型类型参数。

您当前的style函数包含很多代码,内联会将所有行复制到调用位置。最好将函数拆分为invoke部分以捕获reified对象和一个内部部分来创建代理。要从class方法访问internal方法,必须使用inline批注。

@PublishedApi

现在,您可以根据需要创建服务:

companion object {
    inline operator fun <reified T> invoke() =
        createProxy(T::class.java)


    @PublishedApi
    internal fun <T> createProxy(clazz: Class<T>): AppWebServices {
        ...
    }
}