hash hosh
hash hosh

Reputation: 3

cannot get response from api

I have API news It consists of elements web name, key, country It's all good, and I first tested the link : https://newsapi.org/v2/top-headlines?country=eg&apiKey=e80f949de1a34b94804188af28f08f44 And then I built the required class ,and set up a interface and put it Headers , Query ,and when I called it, I found a mistake.

/// 
public class RetrofitClient {

    private static Retrofit retrofit;

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

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


///-----


public interface GetData {

      String API_KEY="e80f949de1a34b94804188af28f08f44";

       @Headers("X-Api-Key:"+API_KEY)
       @GET("/v2/top-headlines")
       Call<List<Response>>getAllHeadlines(@Query("country") String country);

}
//- MainActivity 

   GetData service = RetrofitClient.getRetrofitInstance().create(GetData.class);
      Call<List<Response>> call = service.getAllHeadlines("eg");

        call.enqueue(new Callback<List<Response>>(){
            @Override
            public void onResponse(Call<List<Response>> call, retrofit2.Response<List<Response>> response) {
                Log.d("print","Don"+response.body());
            }

            @Override
            public void onFailure(Call<List<Response>> call, Throwable t) {
               Log.d("print","nON"+t.getMessage());
            }
        });


// Log.d 

 D/print: nONExpected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Upvotes: 0

Views: 275

Answers (1)

alexsanderfr
alexsanderfr

Reputation: 56

This is related to the Gson Converter. You're expecting a List but the json is returning a json object instead of a json array right at the start. Check out your model so you can change it according to json.

Upvotes: 1

Related Questions