Search code examples
javaandroidjsongsonretrofit

How i can parsing json object with retrofit and recyclerView


I'm using GitHub API to show in my application the most starred repository and their names and avatar and description in recyclerView but when I lunch the app everything working but the avatar_url and login return Null.

this is a JSON from Github API

https://api.github.com/search/repositories?q=created:%3E2019-10-01&sort=stars&order=desc

I tried this : client class:

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Client {
    public static final String BASE_URL="https://api.github.com";
    public static Retrofit retrofit=null;

    public static Retrofit getClient()
    {
        if(retrofit==null)
        {
            retrofit=new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();

        }
        return retrofit;
    }
}

Service Class:

package com.example.gethubapi.api;

import com.example.gethubapi.model.ItemResponse;

import retrofit2.Call;
import retrofit2.http.GET;

public interface Service {

    @GET("/search/repositories?q=created:>2017-10-22&sort=stars&order=desc&page=2")
    Call<ItemResponse> getItems();
}

Item class here is the problem if u checked the jSON file in link above you will find a child object from item called owner and i cant select the name of avatar_url and owner name directly

 import com.google.gson.annotations.SerializedName;

    public class Item {
        @SerializedName("avatar_url")
        @Expose
        private String avatarUrl;

        @SerializedName("name")
        @Expose
        private String name;

        @SerializedName("description")
        @Expose
        private String description;

        @SerializedName("login")
        @Expose
        private String owner;

        @SerializedName("stargazers_count")
        @Expose
        private int stargazers;

        public Item(String avatar_url,String name,String description,String owner,int stargazers )
        {
            this.avatarUrl=avatar_url;
            this.name=name;
            this.description=description;
            this.owner=owner;
            this.stargazers=stargazers;

        }
        public String getAvatarUrl()
        {
            return avatarUrl;
        }
        public String getName()
        {
            return name;
        }
        public String getDescription()
        {
            return description;
        }
        public String getOwner()
        {
            return owner;
        }
        public int getStargazers()
        {
            return stargazers;
        }
    }

Solution

  • Looking at the JSON response, the Owner object is part of the Item object. meaning it's nested in the Item object.

    public class Owner {
    @SerializedName("avatar_url")
    @Expose
    private String avatarUrl;
    
    @SerializedName("login")
    @Expose
     private String login;
    
     public Owner(){}
    
     public void setAvatarUrl(String avatar_URL){
     this.avatarUrl = avatar_URL;
     }
    
     pubic String getAvatarUrl(){
     return avatarUrl;
     }
    
     public String getLogin(){
      return login;
     }
    
     public void setLogin(String login){
      this.login = login;}
      }
    
    public class Item {
        @SerializedName("name")
        @Expose
        private String name;
    
        @SerializedName("description")
        @Expose
        private String description;
    
        @SerializedName("owner")
        @Expose
        private Owner owner;
    
         @SerializedName("stargazers_count")
        @Expose
        private int stargazers;
        .........
        }