小编Mik*_*ick的帖子

Kotlin从其他类调用成员扩展函数

我想在多个类中使用此函数:

fun <T> T?.ifNull(function: (T?, s:String) -> Unit) {
}
Run Code Online (Sandbox Code Playgroud)

我怎么能做到这一点?

这就是我想用它的方式:

class A{
fun <T> T?.ifNull(function: (T?, s:String) -> Unit) {
    }
}

class B{
constructor(){
    val a = A()
    //I want to use the function here
}}
Run Code Online (Sandbox Code Playgroud)

android kotlin

7
推荐指数
1
解决办法
1197
查看次数

燃料在错误时得到身体反应

我怎样才能做到这一点?这是我在做的请求。

Fuel.get(url)
    .timeout(TIMEOUT)
    .body(myRequest.parameters!!, Charsets.UTF_8)
    .responseString() { _, response, result ->
                Log.e("result", "$result")
                when (result) {
                    is Result.Success -> {
                        completionHandler(result.get(), null)
                    }
                    is Result.Failure -> {
                        Log.e("fail", "${response}")
                        completionHandler(null, Error(response.responseMessage))
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

我得到的回应是:

Response : Bad Request
Length : 122
Body : ({"message":"You have already booked that iceCream"})
Headers : (9)
Connection : [keep-alive]
Content-Length : [122]
Content-Type : [application/json; charset=utf-8]
Date : [Thu, 03 May 2018 07:33:21 GMT]
Server : [nginx/1.12.1]
Run Code Online (Sandbox Code Playgroud)

我想将正文消息作为我的错误返回。

android kotlin

6
推荐指数
2
解决办法
4742
查看次数

标签 统计

android ×2

kotlin ×2