所以我使用Retrofit 2来创建我的所有API调用。
这是我项目中的一些示例代码。首先是Api。
public class Api {
public static final String BASE_URL = "http://www.api.com/";
public static final String kApiUserLogin = "user/login/";
public static final String kApiUserRegister = "user/register/";
private static ApiInterface sService;
public static ApiInterface client() {
if (sService == null) {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.create();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
sService = retrofit.create(ApiInterface.class);
}
return sService;
}
public interface ApiInterface {
@POST(kApiUserLogin)
Call<UserResponse> loginUser(@Body UserDTO userDTO);
@POST(kApiUserRegister)
Call<UserResponse> registerUser(@Body UserDTO userDTO);
}}
}
然后这就是我使用它的方式:
Call<UserResponse> call = Api.client().registerUser(user);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Response<UserResponse> response) {
UserResponse userResponse = response.body();
if (userResponse.getError() == null) {
UserDTO userDto = userResponse.getUser();
User.getInstance().setUserDTO(userDto);
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
} else {
dialog
.setTitleText("Error")
.setContentText(userResponse.getError())
.setConfirmText("OK")
.changeAlertType(SweetAlertDialog.ERROR_TYPE);
}
}
@Override
public void onFailure(Throwable t) {
Log.d("CallBack", " Throwable is " + t);
dialog.hide();
}
});
Call<UserResponse> call = Api.client().loginUser(user);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Response<UserResponse> response) {
UserResponse userResponse = response.body();
if (userResponse.getError() == null) {
UserDTO userDto = userResponse.getUser();
User.getInstance().setUserDTO(userDto);
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
} else {
dialog
.setTitleText("Error")
.setContentText(userResponse.getError())
.setConfirmText("OK")
.changeAlertType(SweetAlertDialog.ERROR_TYPE);
}
}
@Override
public void onFailure(Throwable t) {
Log.d("CallBack", " Throwable is " + t);
dialog.hide();
}
});
}
现在的问题是我该如何制作它以便我可以在else
内的if (userResponse.getError() == null)
onReponse
部分重用一段代码?别担心,这只是一个例子。整个应用程序中有很多api调用,所以我不能在下面这两种方法中调用方法。
我正在寻找一个默认的onResponse处理程序或类似Api类中的东西?
与此同时,我当然希望对onFailure
做同样的事情。
也许我过于复杂的事情......
答案 0 :(得分:0)
如果我理解正确,这就足够了。
Call<UserResponse> call = Api.client().registerUser(user);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Response<UserResponse> response) {
processResponse(response.body());
}
@Override
public void onFailure(Throwable t) {
Log.d("CallBack", " Throwable is " + t);
dialog.hide();
}
});
Call<UserResponse> call = Api.client().loginUser(user);
call.enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Response<UserResponse> response) {
processResponse(response.body());
}
@Override
public void onFailure(Throwable t) {
Log.d("CallBack", " Throwable is " + t);
dialog.hide();
}
});
}
private void processResponse(UserResponse userResponse) {
if (userResponse.getError() == null) {
UserDTO userDto = userResponse.getUser();
User.getInstance().setUserDTO(userDto);
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
} else {
dialog
.setTitleText("Error")
.setContentText(userResponse.getError())
.setConfirmText("OK")
.changeAlertType(SweetAlertDialog.ERROR_TYPE);
}
}