Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin-2设置POST参数 接口api接口{ @标题(“内容类型:application/json”) @POST(“testgetmemes/”) fun getMemes():调用 } 对象客户端{ var BASE_URL:String=”https://www.androidisapos.com/" val getClient:api接口 得到(){ val gson=GsonBuilder() .setLenient() .create() val client=OkHttpClient.Builder().build() val reformation=reformation.Builder() .baseUrl(基本URL) .客户(客户) .addConverterFactory(GsonConverterFactory.create(gson)) .build() 返回reformation.create(apinterface::class.java) } }_Kotlin_Retrofit_Retrofit2 - Fatal编程技术网

Android Kotlin-2设置POST参数 接口api接口{ @标题(“内容类型:application/json”) @POST(“testgetmemes/”) fun getMemes():调用 } 对象客户端{ var BASE_URL:String=”https://www.androidisapos.com/" val getClient:api接口 得到(){ val gson=GsonBuilder() .setLenient() .create() val client=OkHttpClient.Builder().build() val reformation=reformation.Builder() .baseUrl(基本URL) .客户(客户) .addConverterFactory(GsonConverterFactory.create(gson)) .build() 返回reformation.create(apinterface::class.java) } }

Android Kotlin-2设置POST参数 接口api接口{ @标题(“内容类型:application/json”) @POST(“testgetmemes/”) fun getMemes():调用 } 对象客户端{ var BASE_URL:String=”https://www.androidisapos.com/" val getClient:api接口 得到(){ val gson=GsonBuilder() .setLenient() .create() val client=OkHttpClient.Builder().build() val reformation=reformation.Builder() .baseUrl(基本URL) .客户(客户) .addConverterFactory(GsonConverterFactory.create(gson)) .build() 返回reformation.create(apinterface::class.java) } },kotlin,retrofit,retrofit2,Kotlin,Retrofit,Retrofit2,在函数中: interface ApiInterface { @Headers("Content-Type: application/json") @POST("testgetmemes/") fun getMemes(): Call<List<Memes>> } object ApiClient { var BASE_URL:String="https://www.androidisap

在函数中:

interface ApiInterface {
    @Headers("Content-Type: application/json")
    @POST("testgetmemes/")
    fun getMemes(): Call<List<Memes>>
}

object ApiClient {
    var BASE_URL:String="https://www.androidisapos.com/"
    val getClient: ApiInterface
        get() {

            val gson = GsonBuilder()
                .setLenient()
                .create()

            val client = OkHttpClient.Builder().build()

            val retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build()

            return retrofit.create(ApiInterface::class.java)
        }
}
val调用:call=ApiClient.getClient.getMemes()
排队(对象:Callback{
覆盖fun onResponse(调用:调用?,响应:响应){
setMemes(JSONArray(Gson().toJson(response.body()),gal)
}
覆盖失效时的乐趣(调用:调用?、t:可丢弃?){
Log.d(tagg,t!!.toString())
}
})
如何添加POST参数(以及ofc的值)?我看过无数的例子,但它们都以不同的方式构造了这个绝对糟糕的库的代码,这使得当您不了解Kotlin/Java 100%时无法理解

编辑:

我试过:

    val call: Call<List<Memes>> = ApiClient.getClient.getMemes()
    call.enqueue(object : Callback<List<Memes>> {

        override fun onResponse(call: Call<List<Memes>>?, response: Response<List<Memes>>) {
            setMemes(JSONArray(Gson().toJson(response.body())), gal)
        }

        override fun onFailure(call: Call<List<Memes>>?, t: Throwable?) {
            Log.d(tagg, t!!.toString())
        }
    })
fun getMemes(@Query(“test”)test:String?):调用

val调用:call=ApiClient.getClient.getMemes(“bla”)

它不会发送带有值的POST key
test
bla

将参数添加到您的
getMemes()
接口函数中。用
@Query
@Body
@BenP注释这些参数。检查问题edit pleaseSorry,对于
POST
,它将是
@Field
而不是
@Query
Ok。现在它可以工作了,感谢您将参数添加到
getMemes()
接口函数。用
@Query
@Body
@BenP注释这些参数。请检查问题编辑请求,对于
POST
,它将是
@Field
而不是
@Query
确定现在可以了谢谢
fun getMemes(@Query("test") test: String?): Call<List<Memes>>
val call: Call<List<Memes>> = ApiClient.getClient.getMemes("bla")