java.lang.IllegalArgumentException:服务方法不能返回void

时间:2018-11-22 09:33:37

标签: android kotlin retrofit2

我正在用Kotlin编写,并试图通过以下调用将Retrofit2集成到我的代码中:https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=Hello

这是我的界面:

SELECT
    amount,
    business_dttm,
    moving_sum
FROM
(
    WITH 3 AS window_size
    SELECT
        groupArray(amount) AS amount_arr,
        groupArray(business_dttm) AS business_dttm_arr,
        arrayCumSum(amount_arr) AS amount_cum_arr,
        arrayMap(i -> if(i < window_size, NULL, amount_cum_arr[i] - amount_cum_arr[(i - window_size)]), arrayEnumerate(amount_cum_arr)) AS moving_sum_arr
    FROM
    (
        SELECT *
        FROM A
        ORDER BY business_dttm ASC
    )
)
ARRAY JOIN
    amount_arr AS amount,
    business_dttm_arr AS business_dttm,
    moving_sum_arr AS moving_sum

这是我的MainActivity:

interface MyApiService {
    companion object {
        val myApiService by lazy {
            RealiApiService.create()
        }

        private fun create(): MyApiService {
            val retrofit = Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl("https://en.wikipedia.org/w/")
                    .build()

            return retrofit.create(RealiApiService::class.java)
        }
    }

    fun executeCall(call: Call<Any>) {
        call.enqueue(object : Callback<Any> {
            override fun onResponse(call: Call<Any>, response: Response<Any>) {
                Log.d("response is ${response.body()}")
            }

            override fun onFailure(call: Call<Any>, t: Throwable) {
                Log.d("Throwable is $t")
            }
        })
    }

    @GET("api.php")
    fun searchArtist(@Query("action") action: String,
                     @Query("format") format: String,
                     @Query("list") list: String,
                     @Query("srsearch") srsearch: String):
            Call<Any>
}

上面的代码运行完美。 请注意,但是,beginSearch和MyApiService的executeCall()中的代码完全相同。如果我注释掉该行并使用它而不是现在使用的行,我将得到“ java.lang.IllegalArgumentException:服务方法不能返回void。”,而且我不确定为什么。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

executeCall不应是MyAPIService的方法。 Retrofit尝试与其他方法一起处理它,并且不能,因为错误消息显示"Service methods cannot return void"。因此它无法创建服务并引发异常。如果没有这一行,就不会创建myApiService

传递给Retrofit的接口应该仅仅是一个接口(没有实现),并且仅包含用@GET / @POST / etc注释的请求方法。

基本上将其放在其他任何地方。