简体   繁体   English

为什么我的 retrofit 调用返回不成功的响应?

[英]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所以基本上我正在使用 retrofit 从名为 calorieNinja 的 api 获取数据,由于某种原因,我一直收到不成功的响应

here is the retrofit code:这是 retrofit 代码:

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.顺便说一句,我正在使用 2 种不同类型的用餐对象,因为一种负责从 api 获取数据,另一种用作 Room 数据库的实体,它们没有相同的参数,所以我决定使用两种不同的而不是仅仅添加 @Ignore对象,而我尝试解决此问题。

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! api 密钥不是真的!

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.尝试添加一个拦截器,以便您可以查看所有调用日志(标头、正文、URL 等)并查看 API 发送的错误是什么。

Add OkHtpp to your grade dependencies:OkHtpp添加到您的成绩依赖项:

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:之后,当您创建 Retrofit 实例时,添加拦截器,应该如下所示:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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