Reputation: 34507
Hi I am using gson
library to map values of response to model. I am doing it like this but it is not mapping values of response to model. I am getting list of 50 models but values in it is zero.
@Provides
@Singleton
fun provideRestApiHelper(
okHttpClient: OkHttpClient,
gson: Gson,
rxJava2CallAdapterFactory: RxJava2CallAdapterFactory): RestApi {
val builder = Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addCallAdapterFactory(rxJava2CallAdapterFactory)
.addConverterFactory(GsonConverterFactory.create(gson))
val retrofit = builder.client(okHttpClient).build()
return retrofit.create(RestApi::class.java)
}
RestApi.kt
interface RestApi {
@GET(ApiEndPoint.ENDPOINT_GITHUB_JOBS)
fun getJobsApiCall(): Observable<List<JobsResponse>>
}
ApiHelperImpl.kt
class ApiHelperImpl @Inject constructor(private val restApi: RestApi) : ApiHelper {
override fun getJobsApiCall(): Observable<List<JobsResponse>> {
return restApi.getJobsApiCall()
}
}
JobsResponse.kt
data class JobsResponse(
@field:SerializedName("company_logo")
val companyLogo: String?,
@field:SerializedName("how_to_apply")
val howToApply: String?,
@field:SerializedName("created_at")
val createdAt: String?,
@field:SerializedName("description")
val description: String?,
@field:SerializedName("location")
val location: String?,
@field:SerializedName("company")
val company: String?,
@field:SerializedName("company_url")
val companyUrl: String?,
@field:SerializedName("id")
val id: String?,
@field:SerializedName("title")
val title: String?,
@field:SerializedName("type")
val type: String?,
@field:SerializedName("url")
val url: String?
) : BaseResponse()
I am calling this API https://jobs.github.com/positions.json. Does anyone know what could be the issue ?
Upvotes: 0
Views: 970
Reputation: 6282
That's because you rely on auto-converted java code
remove @field:SerializedName
changed it to @SerializedName
don't put them in to primary constructor
define them like this:
data class JobsResponse(){
@SerializedName("company_logo")
val companyLogo: String? = null
....
}
Upvotes: 1