0

I have used an another test API and it is work fine, but here when I use the actual API with my KEY it is not getting any data.

My APIService

public interface APIServiceTop {

@GET("v2/top-headlines")
Call<List<ArticlesModel>> getTopObjectsList(
        @Query("country")
        String countryCode,
        @Query("page")
        int pageNumber,
        @Query("apikey")
        String API_KEY
    );
}

My RetroInstance

public class RetroInstance {

public static String BASE_URL = "https://newsapi.org/";

private static Retrofit retrofit;
public static HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

public static Retrofit getRetroClientTop() {
    logging.level(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(logging)
            .build();

    if(retrofit == null ) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
        return retrofit;
    }

and here's a link from document, I made like what they wrote. document

Moataz
  • 495
  • 3
  • 20
  • I would advise you to add logging to your retrofit instance. This way you can see exactly what you are getting from the server in response and whether you are getting any response at all. If you don't know how to do it, here is link with explanation: https://stackoverflow.com/questions/21886313/how-to-log-request-and-response-body-with-retrofit-android It is also possible that you have exceeded the daily limit for api requests. – Nemanja Jun 14 '21 at 22:15
  • Thank you, I added it but it is also not getting any data. I have updated my classes to the new one – Moataz Jun 14 '21 at 22:59
  • So, u don't see any data in response, but you are getting response code 200, right? – Nemanja Jun 14 '21 at 23:02
  • In my log this show first 2021-06-14 19:10:02.977 12585-12682/com.moataz.mox D/OkHttp: <-- 200 https://newsapi.org/v2/top-headlines?country=us&apikey=2f183557c723441587950875002b2a83 (902ms). After that all the data display but in the logcat. And yeah there's no data display in recyclerView – Moataz Jun 14 '21 at 23:12
  • I have published the project on github. https://github.com/MoatazBadawy/MOX – Moataz Jun 14 '21 at 23:23
  • The problem is in your model class. See the answer below. – Nemanja Jun 15 '21 at 08:25

1 Answers1

0

There is a issue in your Model class. Try with the following code.

//Update your service class code
public interface APIServiceTop {

@GET("v2/top-headlines")
Call<ArticlesModel> getTopObjectsList(
        @Query("country")
        String countryCode,
        @Query("apikey")
        String API_KEY
);
}

//main model
public class ArticlesModel {
private String status;
private int totalResults;
private ArrayList<Articles> articles;
}

public class Articles {

private String urlToImage;
private String title;
private String details;
private String pages;
private String author;
private String content;
private String url;

public Articles(String urlToImage, String title, String description, String name, String author, String content, String url) {
    this.urlToImage = urlToImage;
    this.title = title;
    this.details = description;
    this.pages = name;
    this.author = author;
    this.content = content;
    this.url = url;
}

public String getUrlToImage() {
    return urlToImage;
}

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

public String getTitle() {
    return title;
}

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

public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}

public String getPages() {
    return pages;
}

public void setPages(String pages) {
    this.pages = pages;
}

public String getAuthor() {
    return author;
}

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

public String getContent() {
    return content;
}

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

public String getUrl() {
    return url;
}

public void setUrl(String url) {
    this.url = url;
}
}
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
  • 1
    Inside Aticles.java class you are missing Source object. – Nemanja Jun 15 '21 at 08:25
  • 1
    Also, the return type should be Call, not Call> – Nemanja Jun 15 '21 at 08:43
  • Thank you guys for your real help. it is worked now. Also another tip for any one who will seen this question, you can use plugin name "json to file", just copy your json file and make new kotlin json file then past in your class. Then it is will create all the classes for you. – Moataz Jun 17 '21 at 01:12