Я всегда работаю с Retrofit и API, как показано ниже:
1) Сначала я определяю все API в специальном намерении (Первый шаг)
2) Затем я создаю контроллер для каждого API (второй шаг)
3) , а затем, когда я хочу использовать их, я делаю, как показано ниже: (третий шаг)
Первый шаг
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);
}
}
Второй шаг
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());
}
});
}
}
Третий шаг
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);
}
}