简体   繁体   中英

why is my retrofit call is returning an unsuccessful response?

so basically am using retrofit to get data from an api called calorieNinja and for some reason i keep getting an unsuccessful response

here is the retrofit code:

retrofit = new Retrofit.Builder().baseUrl("https://api.calorieninjas.com/v1/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            ApiCalorieNinjas apiCalorieNinjas = retrofit.create(ApiCalorieNinjas.class);
            Call<MealCalories> call = apiCalorieNinjas.getMeal("nutrition?query= 5 eggs");
            call.enqueue(new Callback<MealCalories>() {
                @Override
                public void onResponse(Call<MealCalories> call, Response<MealCalories> response) {
                    if(!response.isSuccessful()){
                        Toast.makeText(getContext(),"Not Found",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    mealEaten = response.body();
                    Meal meal1 = new Meal(mealEaten.getName(),mealEaten.getCalories(),mealEaten.getProtein_g(),mealEaten.getCarbohydrates_total_g());
                    mealViewModel.insertMeal(meal1);
                }

                @Override
                public void onFailure(Call<MealCalories> call, Throwable t) {

                }
            });
        }
    });

btw am using 2 different types of meal objects because one is responsible of getting the data from the api and one is used as an entity for Room databse and they dont have the same parameters so instead of just adding @Ignore i decided to use two different objects while i try fixing this problem.

and here is the interface of it:

public interface ApiCalorieNinjas {
@Headers("X-Api-Key: PiQfBb0CZy2GfOZWjWyj6Tg==EdGjoESjqxQh1q4M")
@GET("{meal}")
public Call<MealCalories> getMeal(@Path("meal") String meal);

the api key isnt real!

if additional code is needed please let me know!

Try to add an interceptor so you can see all calls logs (headers, body, URLs, etc...) and see what it's the error that the API is sending.

Add OkHtpp to your grade dependencies:

implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.2"
implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2"

And after that, when you create your Retrofit instance, add the interceptor, should look something like this:

val httpClient = OkHttpClient.Builder()
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
httpClient.addInterceptor(interceptor)
httpClient.addInterceptor(Interceptor { chain: Interceptor.Chain ->
   val original: Request = chain.request()
   val request: Request = original.newBuilder()
        .header("Content-Type", "application/json")
        .method(original.method, original.body)
        .build()
   chain.proceed(request)
})
      
val okHttpClient = httpClient.build()
val retrofit = Retrofit.Builder()
            .baseUrl("https://api.calorieninjas.com/v1/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM