jo jo
jo jo

Reputation: 1838

How to handle multiple response with retrofit - Android

With retrofit I get response LevelsEntity but if I get error it get me ResponseError, NOTE: I cant merge LevelsEntity and ResponseError together in one entity.

LevelsEntity:

class LevelsEntity : ArrayList<LevelsEntityItem>()

LevelsEntityItem:

data class LevelsEntityItem(
    @SerializedName("category")
    val category: Int? = null,
    @SerializedName("completed")
    val completed: Boolean? = null,
    @SerializedName("completionhascriteria")
    val completionhascriteria: Boolean? = null
)

ResponseError:

data class ResponseError(
    @SerializedName("errorcode")
    val errorcode: String? = null,
    @SerializedName("exception")
    val exception: String? = null,
    @SerializedName("message")
    val message: String? = null
)

And I create bellow class for get multiple data like bellow:

class BaseLevelsEntity<LevelsEntity, ResponseError> {
    var levelsEntity: LevelsEntity? = null
    var responseError: ResponseError? = null
    val isSuccess: Boolean
        get() = responseError == null
}

And in my @POST of retrofit is:

@POST("/webservice/rest/server.php")
suspend fun getPopularLevelsInLessonsF(
    @Query("mdwsrestformat") mdwsrestformat: String?,
    @Field("wsfunction") wsfunction: String?,
    @Field("wstoken") wstoken: String?,
    @Field("userid") userid: Int?
): Call<BaseLevelsEntity<LevelsEntity, ResponseError>>

But I cant get any result in my impl:

class LessonsRepositoryImpl(
    private val lessonsRemoteDatasource: LessonsRemoteDatasource
) : LessonsRepository {
    override suspend fun getLevelsInLessonsF(
        wstoken: String,
        userid: Int
    ): Resource<BaseLevelsEntity<LevelsEntity, ResponseError>> {
        return responseToResource(lessonsRemoteDatasource.getLevelsValueInLessonsF(wstoken, userid).execute())
    }

    private fun responseToResource(response: Response<BaseLevelsEntity<LevelsEntity, ResponseError>>): Resource<BaseLevelsEntity<LevelsEntity, ResponseError>> {

        if (response.isSuccessful) {
            if (response.body() != null) {
                response.body()?.let { result ->
                    if (!result.levelsEntity.isNullOrEmpty()) {
                        if (result.levelsEntity!!.size > 0) {
                            return Resource.Success(result)
                        }
                    } else if (result.responseError != null) {
                        return Resource.Error(result.responseError?.errorcode ?: "unknown")
                    }
                }
            } else {
                return Resource.Error("unknown_info")
            }
        }
        return Resource.Error(response.message())
    }
}

Upvotes: 0

Views: 409

Answers (1)

Kamal
Kamal

Reputation: 355

Normally response should be in common format.

If cannot do this from backend then you can receive response as JsonObject and then check the key manually in repository to decide if it is success or error response. Based on that you can then convert the response to object with gson.

Upvotes: 1

Related Questions