skip to Main Content

My App class:

class App(

var id: Int,
var name: String,
var description: String,
var url: String,
var packageName: String,
var appVersion: String,
var iconMediaUUID: String?,
var created: String,
var createdBy: String

) : Serializable

E D I T: I also tried the @SerializedName annotation, no success.

My Retrofit API call:

  @GET("/api/app-list")
  suspend fun getApplications(): Response<List<App>>

The JSON returned from the API:

[
    {
        "id": 1,
        "name": "facebook",
        "description": "face book",
        "url": "https://play.google.com/store/apps/details?id=com.facebook.katana",
        "packageName": "com.facebook.katana",
        "appVersion": "1",
        "iconMediaUUID": null,
        "created": "2023-09-05 09:20",
        "createdBy": "[email protected]"
    },
    {
        "id": 3,
        "name": "twitter",
        "description": "X",
        "url": "https://play.google.com/store/apps/details?id=com.twitter.android",
        "packageName": "com.twitter.android",
        "appVersion": "43",
        "iconMediaUUID": "e2913bc1-4963-4a3e-85bf-8244b5b42979",
        "created": "2023-09-05 10:11",
        "createdBy": "[email protected]"
    }
]

My Retrofit instance:

private fun getBackendClient(): Retrofit =
        Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(BACKEND_BASE_URL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    fun callBackend(): Endpoints {
        return getBackendClient().create(Endpoints::class.java)
    }

Code for logging:

   val response: Response<List<App>> = API.callBackend().getApplications()

   if (response.isSuccessful) {
                val appList = response.body()
                if (appList != null) {
                    Log.i("PARSED_", "size of list: "+appList.size)
                    for (app in appList) {
                        Log.i("PARSED_", "id: " + app.id + ", name: " + app.name)
                    }                        
                } 
             }

Yet, still my parsed instances only have null values in every attribute, why?

Log:

PARSED_  size of list: 2
PARSED_  id: null, name: null
PARSED_  id: null, name: null

Why is it not working?

2

Answers


  1. Chosen as BEST ANSWER

    It turned out I had minifyEnabled true ini my build.gradle (Module version, not project version) file.

    So I'll have to set it false or add a rule to proguard-rules.pro file to skip my App file (and also every other model class) for obfuscation.


  2. Another work around, you can use @Keep for each of model class like this.

    @Keep
    class User {
    
    private String name;
    } 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search