3

I am using Retrofit in an Android project, and wanted to know how people structure their APIs in Retrofit.

So I have some APIs which have same base URL, now do I place all of them in a single interface? Or I place them depending on use case? Like Signing and Authentication goes in one, getting news feed data and doing relevant actions goes in another?

ateebahmed
  • 183
  • 5
  • 19
  • It is a matter of preference and how you'd want to organize the Retrofit calls. I keep all my calls in a single interface file and call it projectnameApiService. If you've got a lot of calls going then it makes sense in splitting those calls into separate files. You can then decide to place this interface file in your data repository package or network package if you have a dedicated package for network related tasks in the app. – MD Naseem Ashraf Mar 17 '19 at 03:45
  • If you have multi APIs that all have a same base URL , add all of your APIs in one Intent , I hope my codes be helpful – reza shah Mar 17 '19 at 04:15

1 Answers1

2

I always work with Retrofit and APIs like below:

1 ) First I define all APIs in a special Intent (First Step)

2 ) Then I create a controller for each API (Second Step)

3 ) and then when I want to use them I do like below : (Third Step)

First Step

public interface OnlineShopAPI {

   String BASE_URL = "https://api.backtory.com/";


   @Headers({"X-Backtory-Authentication-Id:5a154d2fe4b03ffa0436a534"})
   @POST("auth/users")
   Call<User> registerUser(@Body User user);

   interface RegisterUserCallBack {

      void onResponse(boolean successful, String errorMessage, User user);

      void onFailure(String cause);

    }
}

Second Step

 public class RegisterUserController {

     OnlineShopAPI.RegisterUserCallBack registerUserCallBack;

     public RegisterUserController(OnlineShopAPI.RegisterUserCallBack registerUserCallBack) {
    this.registerUserCallBack = registerUserCallBack;
}

     public void start(User user){
         Retrofit retrofit = new Retrofit.Builder()
                 .addConverterFactory(GsonConverterFactory.create())
                 .baseUrl(OnlineShopAPI.BASE_URL)
                 .build();

         OnlineShopAPI onlineShopAPI = retrofit.create(OnlineShopAPI.class);

         Call<User> call = onlineShopAPI.registerUser(user);
         call.enqueue(new Callback<User>() {
             @Override
             public void onResponse(Call<User> call, Response<User> response) {

                 Log.d("TAG" , "Response : " + response.code());

                 if (response.isSuccessful()){
                     registerUserCallBack.onResponse(true , null      ,response.body());
                 }else  {

                    registerUserCallBack.onResponse(false , errorResponse.getMessage() , null);

                 }
             }

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

                 Log.d("TAG" , "onFailure : " + t.getCause());

                 registerUserCallBack.onFailure(t.getCause().getMessage());
             }
         });

     }

 }

Third Step

public class RegisterFragment extends Fragment {

private EditText username;
private EditText password;
private EditText email;

private Button register;
private MenuInflater inflater1;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_register , container ,false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    findViews(view);

    register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            RegisterUserController userController = new RegisterUserController(registerUserCallBack);
            User user = new User();
            user.setPassword(password.getText().toString());
            user.setUsername(username.getText().toString());
            user.setEmail(email.getText().toString());
            userController.start(user);
        }
    });
}

OnlineShopAPI.RegisterUserCallBack registerUserCallBack = new OnlineShopAPI.RegisterUserCallBack() {
    @Override
    public void onResponse(boolean successful , String errorMessage ,User user) {

        if (successful){
            Toast.makeText(getActivity() , "DONE " + user.getUsername() , Toast.LENGTH_LONG).show();
            loginUser();
        }else {
            Toast.makeText(getActivity() , errorMessage , Toast.LENGTH_LONG).show();
        }
    }


    @Override
    public void onFailure(String cause) {

        Toast.makeText(getActivity() , cause , Toast.LENGTH_LONG).show();

    }
};

private  void findViews(View view){

    username = view.findViewById(R.id.username);
    password = view.findViewById(R.id.password);
    email = view.findViewById(R.id.email);

    register = view.findViewById(R.id.register);

  }
}
reza shah
  • 71
  • 6
  • Here if I have large number of API calls, 2nd step will be common for all the calls. Am I correct? – sejn Oct 10 '20 at 03:54