我正在Android项目中使用Retrofit,想了解人们如何在Retrofit中构造其API。
因此,我有一些具有相同基础URL
的API,现在我将它们全部放在一个interface
中吗?还是根据使用情况放置它们?就像签名和身份验证合而为一,获取新闻提要数据并执行相关操作又是另一个?
答案 0 :(得分:1)
我始终使用Retrofit和API,如下所示:
1)首先,我以特殊的Intent(第一步)定义所有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);
}
}