簡體   English   中英

Kotlin-vararg->更改查詢參數值

[英]Kotlin - vararg -> change query param values

在我的android應用中,我使用Retrofit 2

def RETROFIT_VERSION = '2.6.0'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    api 'com.squareup.okhttp3:logging-interceptor:3.8.0'
    api "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
    api "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"

這是我的服務:

public enum OperationType {
      PAYMENT,   PAYOUT,  TRANSFER

}



import retrofit2.Call
import retrofit2.http.*
import java.util.*

@GET("/operations")
    fun getOperationsList(
        @Query("types") typeList: List<@JvmSuppressWildcards OperationType>,
        @Query("status") statusList: List<@JvmSuppressWildcards OperationStatus>,
        @Query("from") from: Date, @Query("to") to: Date
    ): Call<List<Operation>>

在這里使用:

val call = restClient.getOperationsList(
            typesList,
            statusList = operationStatusList,
            from = from,
            to = to
        )

結果如下:

 D/OkHttp  (24112): --> GET http://my_ip:8081/operations?types=payment&types=payout&status=executed&from=2019-08-07T00%3A00%3A00&to=2019-08-07T23%3A59%3A59 http/1.1

如您所見,參數“ type ”通過2次。 尼斯。

現在,我像這樣使用vararg參數:

@GET("/operations")
    fun getOperationsList(
        vararg @Query("types") typeList: List<@JvmSuppressWildcards OperationType>,
        @Query("status") statusList: List<@JvmSuppressWildcards OperationStatus>,
        @Query("from") from: Date, @Query("to") to: Date
    ): Call<List<Operation>>

現在結果是:

D/OkHttp  (25017): --> GET http://my_ip:8081/operations?types=%5Bpayment%2C%20payout%5D&status=executed&from=2019-08-07T00%3A00%3A00&to=2019-08-07T23%3A59%3A59 http/1.1

如您所見,參數“ type ”現在以不同的方式傳遞。 如果我解碼URL我得到

http://my_ip:8081/operations?types=[payment, payout]&status=executed&from=2019-08-07T00:00:00&to=2019-08-07T23:59:59 http/1.1

我只需要這樣傳遞參數“ type ”: type = value&type = value2 ...像這樣:

D/OkHttp  (24112): --> GET http://my_ip:8081/operations?types=payment&types=payout&status=executed&from=2019-08-07T00%3A00%3A00&to=2019-08-07T23%3A59%3A59 http/1.1

我該如何解決?

使用vararg ,必須將變量的類型設置為單個對象的類型,而不是整個集合的類型。 typeList類型更改為OperationTypes

@GET("/operations")
fun getOperationsList(
    @Query("types") vararg types: OperationTypes,
    @Query("status") statusList: List<@JvmSuppressWildcards OperationStatus>,
    @Query("from") from: Date, @Query("to") to: Date
): Call<List<Operation>>

讓我解釋一下vararg如何處理這些示例:

1)

fun getOperationsList(vararg types: OperationTypes)

在這種情況下, getOperationsList方法接受類型為OperationTypes多個參數。 在其主體內部, types變量實際上是Array<OperationTypes>的類型。

您可以像這樣調用getOperationsList (我假設OperationTypes是一個枚舉類):

getOperationsList(OperationTypes.PAYMENT, OperationTypes.PAYOUT)

// or using the spread operator
val types = listOf(OperationTypes.PAYMENT, OperationTypes.PAYOUT)
getOperationsList(*types.toTypedArray())

在該方法內部, types的值等於:

types = arrayOf(OperationTypes.PAYMENT, OperationTypes.PAYOUT)

2)

fun getOperationsList(vararg types: List<OperationTypes>)

在這種情況下, getOperationsList期望其參數為OperationTypes對象的多個列表 在其內部, types變量是Array<List<OperationsTypes>>的類型。

該函數可以稱為:

getOperationsList(listOf(OperationTypes.PAYMENT, OperationTypes.PAYOUT), listOf(OperationTypes.OTHER_TYPE))

並且里面的types的值等於:

types = arrayOf(listOf(OperationTypes.PAYMENT, OperationTypes.PAYOUT), listOf(OperationTypes.OTHER_TYPE))

這就是為什么您當前的實現無法按需工作的原因。

嘗試這個:

@GET("/operations")
fun getOperationsList(
    @Query("type") types: List<@JvmSuppressWildcards OperationType>,
    @Query("status") statusList: List<@JvmSuppressWildcards OperationStatus>,
    @Query("from") from: Date, @Query("to") to: Date
): Call<List<Operation>>

再舉一個例子: github問題

kotlin 參考 function 帶可變參數並轉換數組<out to list<out< div><div id="text_translate"><p> 我已經根據這個主題<a href="https://stackoverflow.com/questions/62260177/kotlin-geneirc-with-vararg-parameter" rel="nofollow noreferrer">基本問題提出了問題</a></p><p>所以,我想提前問一下。 有人用數組和列表回答了這個問題</p><pre>Class Test&lt;T,V&gt;{ var functionPara:(()-&gt;T)? = null var recallFunctionWithFunction:( (Array&lt;out T&gt;) -&gt; V)? = null constructor(value: ()-&gt;T, recallFunctionWithFunction: (Array&lt;out T&gt;) -&gt; V ){ this.functionPara = value this.recallFunctionWithFunction = recallFunctionWithFunction } inline fun &lt;reified T, V&gt; compose(crossinline f: (Array&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results.toTypedArray()) } } fun &lt;T, V&gt; compose(f: (List&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results) } } } fun runCompose(){ compose(functionPara,recallFunctionWithFunction).invoke() }</pre><p> 但我發現,當我引用帶有 vararg 參數的 function 時</p><pre>fun functionA(vararg:Observable&lt;Any&gt;):LiveData&lt;Boolean&gt;{ } fun functionB():Observable&lt;Any&gt;{ }</pre><p> 當我執行類似::functionA 的操作時,A 的類型將是Array&lt;out Observable&lt;Any&gt;&gt;-&gt;LiveData&lt;Boolean&gt;因此,當我執行類似操作時</p><p>Test&lt;Observable&lt;Any&gt;,LiveData&lt;Boolean&gt;&gt;(::functionB,::functionA).runCompose()</p><p> <strong><em>情況 1</em></strong>如果我使用 compose function 並接受 List 類型,它將顯示類型不匹配,因為引用::functionA 將返回 Array</p><p><img src="https://i.stack.imgur.com/X3k3A.png" alt="圖片1"></p><p> <strong><em>情況 2</em></strong>如果我使用 compose function 並接受 Array 類型,它將顯示錯誤</p><p>不能使用“T”作為具體類型參數。 改用 class</p><p><img src="https://i.stack.imgur.com/Z2iwJ.png" alt="圖片2"></p><p> 在上一篇文章中,有人回答我將數組轉換為列表。 但是如何將引用 function 與 vararg 參數與原始Array&lt;out to List &lt;out ? 當我引用 function 之類的 function 時,類型必須是Array&lt;out但我想將其插入到 compose function 中。 我必須轉換它。 任何人都可以幫忙嗎? 我在那兒呆了很長時間。 希望有人能救我!!</p></div></out>

[英]kotlin reference function with vararg parameter and convert Array<out to List<out

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 kotlin 中的字符串格式和可變參數 kotlin 中的 vararg 沒用嗎? 引用vararg函數參數Kotlin 無法使用Java的vararg參數而不是vararg參數調用函數 將vararg參數傳遞給Kotlin中的另一個函數時編譯時間錯誤 如何在Kotlin的枚舉中檢索用vararg聲明的參數的值 kotlin 參考 function 帶可變參數並轉換數組<out to list<out< div><div id="text_translate"><p> 我已經根據這個主題<a href="https://stackoverflow.com/questions/62260177/kotlin-geneirc-with-vararg-parameter" rel="nofollow noreferrer">基本問題提出了問題</a></p><p>所以,我想提前問一下。 有人用數組和列表回答了這個問題</p><pre>Class Test&lt;T,V&gt;{ var functionPara:(()-&gt;T)? = null var recallFunctionWithFunction:( (Array&lt;out T&gt;) -&gt; V)? = null constructor(value: ()-&gt;T, recallFunctionWithFunction: (Array&lt;out T&gt;) -&gt; V ){ this.functionPara = value this.recallFunctionWithFunction = recallFunctionWithFunction } inline fun &lt;reified T, V&gt; compose(crossinline f: (Array&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results.toTypedArray()) } } fun &lt;T, V&gt; compose(f: (List&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results) } } } fun runCompose(){ compose(functionPara,recallFunctionWithFunction).invoke() }</pre><p> 但我發現,當我引用帶有 vararg 參數的 function 時</p><pre>fun functionA(vararg:Observable&lt;Any&gt;):LiveData&lt;Boolean&gt;{ } fun functionB():Observable&lt;Any&gt;{ }</pre><p> 當我執行類似::functionA 的操作時,A 的類型將是Array&lt;out Observable&lt;Any&gt;&gt;-&gt;LiveData&lt;Boolean&gt;因此,當我執行類似操作時</p><p>Test&lt;Observable&lt;Any&gt;,LiveData&lt;Boolean&gt;&gt;(::functionB,::functionA).runCompose()</p><p> <strong><em>情況 1</em></strong>如果我使用 compose function 並接受 List 類型,它將顯示類型不匹配,因為引用::functionA 將返回 Array</p><p><img src="https://i.stack.imgur.com/X3k3A.png" alt="圖片1"></p><p> <strong><em>情況 2</em></strong>如果我使用 compose function 並接受 Array 類型,它將顯示錯誤</p><p>不能使用“T”作為具體類型參數。 改用 class</p><p><img src="https://i.stack.imgur.com/Z2iwJ.png" alt="圖片2"></p><p> 在上一篇文章中,有人回答我將數組轉換為列表。 但是如何將引用 function 與 vararg 參數與原始Array&lt;out to List &lt;out ? 當我引用 function 之類的 function 時,類型必須是Array&lt;out但我想將其插入到 compose function 中。 我必須轉換它。 任何人都可以幫忙嗎? 我在那兒呆了很長時間。 希望有人能救我!!</p></div></out> Kotlin 中的類型參數回調 function 如何在 Kotlin 的 RecyclerView 中更改每個偶數值元素的顏色? 使用查詢參數改進URL
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM