Api call issue with Retrofit for kotlin using MVP

I am working with kotlin using Retrofit in MVP. The API not return the response or finish the view load.

presenter code

import android.content.Context
import android.util.Log
import com.crosspoles.CrosspolesApp
import com.crosspoles.R
import com.crosspoles.Views.LoginView

import com.ruhe.model.LoginModel

import okhttp3.RequestBody

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*
class LoginPresenter : BasePresenter<LoginView?>() {

    fun LoginUser(activity:Context, map:HashMap<String, RequestBody>, progress:Boolean) {
        view!!.enableLoadingBar(activity, progress, activity.getResources().getString(R.string.loading))

                ?.login(map).enqueue(object:Callback<LoginModel> {
                    override fun onResponse(call:Call<LoginModel>, response:Response<LoginModel>) {
                        view!!.enableLoadingBar(activity, false, "")
                        view!!.onLoginComplete(response.body(), response.code())
                    override fun onFailure(call:Call<LoginModel>, t:Throwable) {
                        view!!.enableLoadingBar(activity, false, "")
                        try {
                        catch (e:Exception) {



private fun <T> Call<T>?.enqueue(callback: Callback<LoginModel>) {


Api service

package com.crosspoles.service

import com.crosspoles.model.LoginModel
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.PartMap
import java.util.*

interface ApiService {
    fun login(@PartMap map: HashMap<String, RequestBody>): Call<LoginModel?>?

CrosspolesApp is an application call inherits with whole projects

package com.crosspoles

import android.content.Context
import android.os.StrictMode
import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication
import com.crosspoles.service.ApiService
import com.crosspoles.service.CustomInterceptor
import com.facebook.stetho.Stetho
import com.google.gson.GsonBuilder
import com.crosspoles.extra.Constants
import io.github.inflationx.calligraphy3.CalligraphyConfig
import io.github.inflationx.calligraphy3.CalligraphyInterceptor
import io.github.inflationx.viewpump.ViewPump
import okhttp3.Cache
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.io.File
import java.util.*
import java.util.concurrent.TimeUnit

class CrosspolesApp : MultiDexApplication() {
    var isAidl = false

    var apiService: ApiService? = null
        private set
    override fun onCreate() {
        instance = this
        val builder = StrictMode.VmPolicy.Builder()
    override fun attachBaseContext(base: Context) {
    fun createApiService(): ApiService? {
        val gson = GsonBuilder().create()
        val httpCacheDirectory = File(cacheDir, "cache_file")
        val cache = Cache(httpCacheDirectory, 20 * 1024 * 1024)
        val okHttpClient = OkHttpClient.Builder()
                .connectTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .connectionPool(ConnectionPool(0, 5 * 60 * 1000, TimeUnit.SECONDS))
                .addInterceptor(CustomInterceptor(instance, Locale.getDefault().language, appVersion))
        val retrofit = Retrofit.Builder().client(okHttpClient)
        apiService = retrofit.create(ApiService::class.java)
        return apiService
    private val appVersion: String
        private get() = try {
            packageManager.getPackageInfo(packageName, 0).versionName
        } catch (e: Exception) {
    companion object {
        var instance: CrosspolesApp? = null
            private set

I want to know what mistake in these. Please notify me if any other code needed to more understand.


According to the discussion in the comments under the question and additionally posted code I assume, and mostly sure, that the issue is the wrong type of callback object passed into the enqueue function.

Currently, the enqueue function receives object of type Callback<LoginModel>:

             ?.login(map).enqueue(object: Callback<LoginModel> { ... })

But according to ApiService function named login returns optional Call object with optional LoginModel response object.

It means that the correct way to call enqueue function would be:

             ?.login(map)?.enqueue(object: Callback<LoginModel?> { ... })

Since login(map) returns optional Call object question mark was placed before calling enqueue.

Make sure you update Callback object onResponse and onFailure functions implementation (note the question marks):

override fun onResponse(call: Call<LoginModel?>, ...) { ... }
override fun onFailure(call: Call<LoginModel?>, ...) { ... }

Remove the empty extension function as it serves no purpose.