0

i am trying to build an app that uses an API that I made with laravel. I managed to setup retrofit, here's how :

protected void configureRetrofit() {
    Gson gson = new GsonBuilder()
        .setLenient()
        .create();

    retrofit = new Retrofit.Builder().baseUrl("http://10.0.2.2:8000/api/")
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

    restaurantApi = retrofit.create(RestaurantApi.class);
    userApi = retrofit.create(UserApi.class);
}

Then i created a method to pass my restaurantApi to another activity :

public static RestaurantApi getRestaurantApi() {
    return restaurantApi;
}

And I was able to retrive that api and realize a post by doing this :

MainActivity.getRestaurantApi().postRestaurant(restaurant).enqueue(.......  etc

I tried to de the exact same thing but with my userApi instead and i got the error message in the title of this post, telling me that userApi is null basically. I'm really confused, i can't find what i'm missing so i'm reaching for some help.

public static UserApi getUserApi() { return userApi; }
MainActivity.getUserApi().authenticateUser(login, password).enqueue(....... etc
Scotty
  • 11
  • 1
  • 6
  • Ok turns out the problem was my `@POST` method, i had two Strings as parameters in the `@BODY` (password and login) but by creating a `LoginRequest` class and filling it with password and login, then sending it to my `@POST` method, it worked. – Scotty Feb 27 '22 at 20:39

0 Answers0