Search code examples
androidjsongsonretrofit2jsonschema2pojo

How do i fetch data as a List from json using Retrofit


Single data are fetched but when i try to fetch as a List it's shown the problem Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ here is the JSON link: https://newsapi.org/v2/top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc

Here is my Method to get JSON Data in my MainActivity

   private void loadJSON_Data() {

    RetofitClient_SingleTone 
      retofitClient_singleTone=RetofitClient_SingleTone.getInstance();


    Api api = retofitClient_singleTone.getApi();

    Call<List<Response_Pojo>> call = api.getAllResponse();

    call.enqueue(new Callback<List<Response_Pojo>>() {
        @Override
        public void onResponse(Call<List<Response_Pojo>> call, 
         Response<List<Response_Pojo>> response) {
            if (response.isSuccessful()) {

                Toast.makeText(context, "Success" + response.body().size(), 
           Toast.LENGTH_SHORT).show();
                Log.e("Home  - --", "onResponse: " + response.body());
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFailure(Call<List<Response_Pojo>> call, Throwable t) {
            Log.e("Home Failed - -", "Failed: " + t.getMessage());
            Toast.makeText(context, "Faield - - " +t.getMessage(), 
             Toast.LENGTH_SHORT).show();
            swipeRefreshLayout.setRefreshing(false);
        }
    });
}

Here is my api.Class

public interface Api {

@GET("top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc")
Call<List<Response_Pojo>> getAllResponse();

}

RetofitClient_SingleTone Class

public class RetofitClient_SingleTone {

  private static final String BASE_URL = "https://newsapi.org/v2/";
  private static RetofitClient_SingleTone mInstance;
  private Retrofit retrofit;

  private RetofitClient_SingleTone() {
    retrofit=new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
              .build();
 }
  public static synchronized  RetofitClient_SingleTone getInstance(){
    if (mInstance==null){
        mInstance = new RetofitClient_SingleTone();
    }
    return mInstance;
 }

 public Api getApi(){
    return retrofit.create(Api.class);
  }
}

Here is my POJO class

public class Response_Pojo {

@SerializedName("status")
@Expose
private String status;
@SerializedName("totalResults")
@Expose
private Integer totalResults;
@SerializedName("articles")
@Expose
private List<Article> articles = null;

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public Integer getTotalResults() {
    return totalResults;
}

public void setTotalResults(Integer totalResults) {
    this.totalResults = totalResults;
}

public List<Article> getArticles() {
    return articles;
}

public void setArticles(List<Article> articles) {
    this.articles = articles;
}
     public class Source {

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

   }

   public class Article {

    @SerializedName("source")
    @Expose
    private Source source;
    @SerializedName("author")
    @Expose
    private String author;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("url")
    @Expose
    private String url;
    @SerializedName("urlToImage")
    @Expose
    private String urlToImage;
    @SerializedName("publishedAt")
    @Expose
    private String publishedAt;
    @SerializedName("content")
    @Expose
    private String content;

    public Source getSource() {
        return source;
    }

    public void setSource(Source source) {
        this.source = source;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrlToImage() {
        return urlToImage;
    }

    public void setUrlToImage(String urlToImage) {
        this.urlToImage = urlToImage;
    }

    public String getPublishedAt() {
        return publishedAt;
    }

    public void setPublishedAt(String publishedAt) {
        this.publishedAt = publishedAt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

   }

   }

Solution

  • Inside the api service interface your response should be the Response_Pojo class instead of List<Response_Pojo> like following:

    public interface Api {
    
    @GET("top-headlines?sources=abc-news&apiKey=4969854e2d424ed9972370f709ace9cc")
    Call<Response_Pojo> getAllResponse();
    
    }
    

    Also need change the Calling API code accordingly.

    MainActivity.class:

    private void loadJSON_Data() {
        RetofitClient_SingleTone 
          retofitClient_singleTone=RetofitClient_SingleTone.getInstance();
    
    
    Api api = retofitClient_singleTone.getApi();
    
    Call<Response_Pojo> call = api.getAllResponse();
    
    call.enqueue(new Callback<Response_Pojo>() {
        @Override
        public void onResponse(Call<Response_Pojo> call, 
         Response<Response_Pojo> response) {
            if (response.isSuccessful()) {
    
                Toast.makeText(context, "Success" + response.body().getArticles().size(), 
           Toast.LENGTH_SHORT).show();
                Log.e("Home  - --", "onResponse: " + response.body());
                swipeRefreshLayout.setRefreshing(false);
            }
        }
    
        @Override
        public void onFailure(Call<Response_Pojo> call, Throwable t) {
            Log.e("Home Failed - -", "Failed: " + t.getMessage());
            Toast.makeText(context, "Faield - - " +t.getMessage(), 
             Toast.LENGTH_SHORT).show();
            swipeRefreshLayout.setRefreshing(false);
        }
    });
    }
    

    Please update code accordingly, i haven't edited it into the IDE.

    Get the list from response by following code inside onResponse method:

    @Override
        public void onResponse(Call<Response_Pojo> call, 
         Response<Response_Pojo> response) {
            if (response.isSuccessful()) {
              list = response.body().getArticles() // get the list from here
    
        }