简体   繁体   English

在Kotlin中自定义实现回调以进行改造

[英]Custom Implementation of Callback for Retrofit in Kotlin

I have recently started with Kotlin for my android development and have faced minor issues which by using documentation I was able to solve, but with Retrofit Callback implementation I am facing a problem. 我最近开始使用Kotlin进行android开发,并且遇到了一些小问题,这些问题可以通过使用文档来解决,但是使用Retrofit Callback实现时,我遇到了问题。 I have a custom implementation for the Retrofit callback that I use in Java: 我在Java中使用了Retrofit回调的自定义实现:

public abstract class RemoteCallback<T> implements Callback<T> {
@Override
public final void onResponse(Call<T> call, Response<T> response) {

    switch (response.code()) {
        case HttpsURLConnection.HTTP_OK:
        case HttpsURLConnection.HTTP_CREATED:
        case HttpsURLConnection.HTTP_ACCEPTED:
        case HttpsURLConnection.HTTP_NOT_AUTHORITATIVE:
            if (response.body() != null) {
                onSuccess(response.body());
            }
            break;
        case HttpURLConnection.HTTP_UNAUTHORIZED:
            onUnauthorized();
            break;
        default:
            onFailed(new Throwable("Default " + response.code() + " " + response.message()));
    }
}

@Override
public final void onFailure(Call<T> call, Throwable t) {
    onFailed(t);
}

public abstract void onSuccess(T responseBody);

public abstract void onUnauthorized();

public abstract void onFailed(Throwable throwable);}

I used the same class in Kotlin Project which in return converted the java code to kotlin code 我在Kotlin Project中使用了相同的类,该类将Java代码转换为Kotlin代码

abstract class RemoteCallback<T> : Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {

    when (response.code()) {
        HttpsURLConnection.HTTP_OK,
        HttpsURLConnection.HTTP_CREATED,
        HttpsURLConnection.HTTP_ACCEPTED,
        HttpsURLConnection.HTTP_NOT_AUTHORITATIVE ->
            if (response.body() != null)
            onSuccess(response.body())
        HttpURLConnection.HTTP_UNAUTHORIZED -> onUnauthorized()
        else -> onFailed(Throwable("Default " + response.code() + " " + response.message()))
    }
}

override fun onFailure(call: Call<T>, t: Throwable) {
    onFailed(t)
}

abstract fun onSuccess(responseBody: T)

abstract fun onUnauthorized()

abstract fun onFailed(throwable: Throwable)}

The problem is when I call onSucess(response.body()) as it says "Smart cast to 'T' is impossible, because 'response.body()' is a complex expression" 问题是当我调用onSucess(response.body())时,它说“不可能强制转换为'T',因为'response.body()'是一个复杂的表达式” 问题 Please guide me on how to fix it 请指导我如何解决它

You are sending a Nullable value to a NonNull function as parameter. 您正在将Nullable值作为参数发送给NonNull函数。 Therefore, you are getting that issue. 因此,您遇到了这个问题。 You can go about this like: 您可以像这样:

interface RemoteCallback<T> : Callback<T> {
    override fun onResponse(call: Call<T>?, response: Response<T>?) {
        when (response?.code()) {
            HttpsURLConnection.HTTP_OK,    
            HttpsURLConnection.HTTP_CREATED,
            HttpsURLConnection.HTTP_ACCEPTED,
            HttpsURLConnection.HTTP_NOT_AUTHORITATIVE ->
                response.body()?.apply { onSuccess(this) }
            HttpURLConnection.HTTP_UNAUTHORIZED -> onUnauthorized()
            else -> onFailed(Throwable("Default " + response?.code() + " " + response?.message()))
        }
    }

    override fun onFailure(call: Call<T>?, t: Throwable?) {
        // handle error mechanism
        t?.apply { onFailed(this) }    
    }
    fun onSuccess(responseBody: T)
    fun onUnauthorized()
    fun onFailed(throwable: Throwable)
}

Alternatively, you can just use onSuccess(responseBody: T?) and your code should work 或者,您可以只使用onSuccess(responseBody: T?) ,您的代码应该可以工作

I have a very similar implementation on my project. 我的项目有一个非常相似的实现。 Try as follow 尝试按照

abstract class RemoteCallback: Callback<ResponseBody> {
    override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {
        onFailed(t)
    }

    override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {
        when (response.code()) {
            HttpsURLConnection.HTTP_OK,
            HttpsURLConnection.HTTP_CREATED,
            HttpsURLConnection.HTTP_ACCEPTED,
            HttpsURLConnection.HTTP_NOT_AUTHORITATIVE ->
                if (response.body() != null)
                onSuccess(response.body())
            HttpURLConnection.HTTP_UNAUTHORIZED -> onUnauthorized()
            else -> onFailed(Throwable("Default " + response.code() + " " + response.message()))
        }
    }

    abstract fun onUnauthorized()

    abstract fun onFailed(throwable: Throwable)
}

Work fine for me 对我来说很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM