简体   繁体   中英

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. I have a custom implementation for the Retrofit callback that I use in Java:

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

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" 问题 Please guide me on how to fix it

You are sending a Nullable value to a NonNull function as parameter. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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