Search code examples
androidkotlingsondata-class

Problem with RetroFit Dataclass, returning null response


When I run the API in browser or without retrofit, it returns a perfect response. But when i pass it through retrofit and gson, it returns null. I thought there must be some problem with the data classes i am using.

My API response

{
"total_count": 1,
"incomplete_results": false,
"items": [
{
"login": "rishavnaskar",
"id": 59786899,
"node_id": "MDQ6VXNlcjU5Nzg2ODk5",
"avatar_url": "https://avatars.githubusercontent.com/u/59786899?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/rishavnaskar",
"html_url": "https://github.com/rishavnaskar",
"followers_url": "https://api.github.com/users/rishavnaskar/followers",
"following_url": "https://api.github.com/users/rishavnaskar/following{/other_user}",
"gists_url": "https://api.github.com/users/rishavnaskar/gists{/gist_id}",
"starred_url": "https://api.github.com/users/rishavnaskar/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/rishavnaskar/subscriptions",
"organizations_url": "https://api.github.com/users/rishavnaskar/orgs",
"repos_url": "https://api.github.com/users/rishavnaskar/repos",
"events_url": "https://api.github.com/users/rishavnaskar/events{/privacy}",
"received_events_url": "https://api.github.com/users/rishavnaskar/received_events",
"type": "User",
"site_admin": false,
"score": 1
}
]
}

PLEASE NOTICE THAT "ITEMS" IS AN ARRAY

My data class

data class SearchPageUserModel(
@SerializedName("total_count") val total_count : Int,
@SerializedName("incomplete_results") val incomplete_results : Boolean,
@SerializedName("items") val items : List<Items>)

data class Items (
@SerializedName("login") val login : String,
@SerializedName("id") val id : String,
@SerializedName("node_id") val node_id : String,
@SerializedName("avatar_url") val avatar_url : String,
@SerializedName("gravatar_id") val gravatar_id : String,
@SerializedName("url") val url : String,
@SerializedName("html_url") val html_url : String,
@SerializedName("followers_url") val followers_url : String,
@SerializedName("following_url") val following_url : String,
@SerializedName("gists_url") val gists_url : String,
@SerializedName("starred_url") val starred_url : String,
@SerializedName("subscriptions_url") val subscriptions_url : String,
@SerializedName("organizations_url") val organizations_url : String,
@SerializedName("repos_url") val repos_url : String,
@SerializedName("events_url") val events_url : String,
@SerializedName("received_events_url") val received_events_url : String,
@SerializedName("type") val type : String,
@SerializedName("site_admin") val site_admin : Boolean,
@SerializedName("score") val score : Int)

IT RETURNS 404 reponse code. Please help me out.


Solution

  • It's solved. Apparently the error was not in the data class.

    It was in the retrofit interface.

    My interface WITH ERROR

    interface RetroFitService {
    @GET("users")
    fun searchUser(@Query("q") q: String): Call<SearchPageUserModel>}
    

    My interface WITHOUT ERROR (SOLVED)

    interface RetroFitService {
    @GET("search/users")
    fun searchUser(@Query("q") q: String): Call<SearchPageUserModel>}