我正在使用api 28 Kotlin开发一个Android应用程序。我还使用swagger获取所有Web服务。大张旗鼓地获得“ Kotlin_client_generation”文件后,将其添加到我的项目中。我想使用以下mobileApi之一:
fun apiMobileAPIGetProviderPost(disc: kotlin.Any, pageIndex: kotlin.Int, pageSize: kotlin.Int, sort: kotlin.String, order: kotlin.String) : kotlin.Array<ProviderData> {
val localVariableBody: kotlin.Any? = disc
val localVariableQuery: MultiValueMap = mapOf("pageIndex" to listOf("$pageIndex"), "pageSize" to listOf("$pageSize"), "sort" to listOf("$sort"), "order" to listOf("$order"))
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf()
val acceptsHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Accept" to "application/json")
val localVariableHeaders: kotlin.collections.MutableMap<kotlin.String,kotlin.String> = mutableMapOf()
localVariableHeaders.putAll(contentHeaders)
localVariableHeaders.putAll(acceptsHeaders)
val localVariableConfig = RequestConfig(
RequestMethod.POST,
"/api/MobileAPI/GetProvider",
query = localVariableQuery,
headers = localVariableHeaders
)
val response = request<kotlin.Array<ProviderData>>(
localVariableConfig,
localVariableBody
)
return when (response.responseType) {
ResponseType.Success -> (response as Success<*>).data as kotlin.Array<ProviderData>
ResponseType.Informational -> TODO()
ResponseType.Redirection -> TODO()
ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error")
ResponseType.ServerError -> throw ServerException((response as ServerError<*>).message ?: "Server error")
else -> throw kotlin.IllegalStateException("Undefined ResponseType.")
}
}
我在活动中添加以下代码:
val params = HashMap<String, String>()
MobileAPIApi().apiMobileAPIGetProviderPost(params,0,50,null,null,
object : Response.Listener<List<ProviderData>>() {
fun onResponse(response: List<ProviderData>?) {
if (response != null && response.size != 0) {......}
我的问题在这里,为什么不能像在Java中那样使用Response.Listener,以及如何替换它。
答案 0 :(得分:1)
以下是在Kotlin
中使用截击的示例:
val stringRequest = object : StringRequest(Request.Method.POST, "url",
com.android.volley.Response.Listener { response ->
if (response != null) {
}
}, com.android.volley.Response.ErrorListener { error -> error.printStackTrace() }) {
@Throws(AuthFailureError::class)
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params["key"] = "value"
return params
}
}
您可以替换参数,并在替换值后使用截击的响应。