'Android Pexels Api integration error using retrofit. Getting 404 error
I am trying to integrate my application with Pexels API by using Retrofit and GSON but without success. I keep getting 404 error and therefore I am getting a null pointer exception on the console.
This is the working api: https://api.pexels.com/v1/curated?page=1&per_page=14
public class ApiClient {
public static final String API_KEY = "xxxxxxxxxx";
public static final String BASE_URL = "https://api.pexels.com/v1/";
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;
}
}
public interface ApiInterface {
@Headers({"Authorization: " + ApiClient.API_KEY})
@GET("/curated")
Call<ExplorePageModelResponse>getExplorePagePhotos(
@Query("page") int page,
@Query("per_page") int per_page
);
}
This is the service class that communicates with the pexels api. The response body is empty...
public class ExplorePageService implements ItemListContract.Model<ExplorePageModelPhotos> {
private final String TAG = "ExplorePageService";
private int pageNo = 1;
private final int PER_PAGE = 14;
@Override
public void getExploreItemList(OnFinishedListener<ExplorePageModelPhotos> onFinishedListener, int pageNo) {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<ExplorePageModelResponse> call = apiService.getExplorePagePhotos(1, PER_PAGE);
call.enqueue(new Callback<ExplorePageModelResponse>() {
@Override
public void onResponse(Call<ExplorePageModelResponse> call, Response<ExplorePageModelResponse> response) {
List<ExplorePageModelPhotos> list = response.body().getExplorePageModelPhotos();
onFinishedListener.onFinished(list);
}
@Override
public void onFailure(Call<ExplorePageModelResponse> call, Throwable t) {
Log.e(TAG, t.toString());
onFinishedListener.onFailure(t);
}
});
}
}
This is the api response:
Response{protocol=h2, code=404, message=, url=https://api.pexels.com/curated?page=1&per_page=14}
I must be doing something wrong in the ApiInterface part since it's all working on the postman side but couldn't figure out. Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
