Home > Software design >  API response using Retrofit returns null for certain parameters which aren't null
API response using Retrofit returns null for certain parameters which aren't null

Time:11-19

I'm using Retrofit for my android application, but few of the parameters come back as null while others have values, it's working fine on POSTMAN

function I'm using for Api Call

private fun apiCall(search: String): List<SortedApiData> {

        var recyclerDataList: List<SortedApiData> = ArrayList<SortedApiData>()

        val consumer = OkHttpOAuthConsumer("9c6c751a78db41b9a8bec92ef28c7656", "3f302d7930a74c1db0304ce675d4d41b")
        consumer.setTokenWithSecret("","")

        val client1 = OkHttpClient.Builder()
            .addInterceptor(SigningInterceptor(consumer))
            .build()

        var retrofit : Retrofit = Retrofit.Builder()
            .baseUrl(BaseUrl)
            .client(client1)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val api: Client = retrofit.create(Client::class.java)
        val call: retrofit2.Call<ApiData?>? = api.getResponse(search, 5, 0, 0)

        call?.enqueue(object : Callback<ApiData?> {
            override fun onFailure(call: retrofit2.Call<ApiData?>, t: Throwable?) {
                Log.v("retrofit", t.toString())
            }

            override fun onResponse(call: retrofit2.Call<ApiData?>, response: Response<ApiData?>) {
                val data: ApiData? = response.body()
                popUprecycler.visibility = View.VISIBLE

                Log.v("retrofit", data.toString())

                recyclerDataList = createList(data)
                adapter = PopUpAdapter(this@PopPage, recyclerDataList)
                popUprecycler.layoutManager = GridLayoutManager(applicationContext, 2)
                popUprecycler.itemAnimator = DefaultItemAnimator()
                popUprecycler.setHasFixedSize(true)
                popUprecycler.adapter = adapter
                Log.i("Shrey", "shit")
            }
        })
        return recyclerDataList
    }

@GET Request function

interface Client {

    @GET("{id}/icons")
    fun getResponse(@Path("id") id: String?,@Query("limit") limit: Int?,@Query("offset") offset: Int?,@Query("page") page: Int?): Call<ApiData?>?
}

Model class

public class ApiData{
    private int total;
    private String generatedAt;
    private Collection collection;
    private List<IconsItem> icons;

    public int getTotal(){
        return total;
    }

    public String getGeneratedAt(){
        return generatedAt;
    }

    public Collection getCollection(){
        return collection;
    }

    public List<IconsItem> getIcons(){
        return icons;
    }
}

Icon Model Class

    public class IconsItem{
    private Sponsor sponsor;
    private String uploaderId;
    private String isActive;
    private String nounjiFree;
    private int year;
    private String dateUploaded;
    private Object sponsorCampaignLink;
    private int termId;
    private String sponsorId;
    private String termSlug;
    private String licenseDescription;
    private List<TagsItem> tags;
    private String attributionPreviewUrl;
    private String updatedAt;
    private String previewUrl;
    private Uploader uploader;
    private String attribution;
    private String previewUrl42;
    private String freemium;
    private String term;
    private String id;
    private String permalink;
    private String isExplicit;
    private String previewUrl84;

    public Sponsor getSponsor(){
        return sponsor;
    }

    public String getUploaderId(){
        return uploaderId;
    }

    public String getIsActive(){
        return isActive;
    }

    public String getNounjiFree(){
        return nounjiFree;
    }

    public int getYear(){
        return year;
    }

    public String getDateUploaded(){
        return dateUploaded;
    }

    public Object getSponsorCampaignLink(){
        return sponsorCampaignLink;
    }

    public int getTermId(){
        return termId;
    }

    public String getSponsorId(){
        return sponsorId;
    }

    public String getTermSlug(){
        return termSlug;
    }

    public String getLicenseDescription(){
        return licenseDescription;
    }

    public List<TagsItem> getTags(){
        return tags;
    }

    public String getAttributionPreviewUrl(){
        return attributionPreviewUrl;
    }

    public String getUpdatedAt(){
        return updatedAt;
    }

    public String getPreviewUrl(){
        return previewUrl;
    }

    public Uploader getUploader(){
        return uploader;
    }

    public String getAttribution(){
        return attribution;
    }

    public String getPreviewUrl42(){
        return previewUrl42;
    }

    public String getFreemium(){
        return freemium;
    }

    public String getTerm(){
        return term;
    }

    public String getId(){
        return id;
    }

    public String getPermalink(){
        return permalink;
    }

    public String getIsExplicit(){
        return isExplicit;
    }

    public String getPreviewUrl84(){
        return previewUrl84;
    }
}

API response notice the highlighted value which shouldn't be null

The correct API response

CodePudding user response:

note that not read variables are these will camelCase naming (big letter in the middle), in the meanwhile in JSON you are using underscore mark _ for separating words. properly read values are only these with exacly same key in JSON and name in model (so no underscore, no big letter in both places). you should use @SerializedName annotation for these fields, e.g.

@SerializedName("attribution_preview_url") // real key in JSON
private String attributionPreviewUrl;

also worth checking setFieldNamingPolicy method, maybe thats may be somehow automated without writting annoatation for every camelCased name (in JSON key with underscores)

  • Related