【发布时间】:2025-12-07 20:30:02
【问题描述】:
我在我的应用程序中使用 .aar 库。 它有一个我需要覆盖的网络委托接口。
ApiResponse executeApiCall(String url, HTTPMethod method, String params)
我正在使用 Retrofit 进行网络调用。我需要将同步调用转换为 USE 异步调用。
@Override
public ApiResponse executeApiCall(String url, HTTPMethod method, String params) {
ApiResponse apiResponse;
try {
// Synchronous Call
Call<String> call = RestClient.get().getStringResponse(url, method, params);
Response<String> response = call.execute();
apiResponse = new ApiResponse(response.code(), response.body());
} catch (Exception e) {
apiResponse = new ApiResponse();
}
return apiResponse;
}
现在我被困在如何在必须覆盖的网络接口中使用异步调用。
@Override
public ApiResponse executeApiCall(String url, HTTPMethod method, String params) {
ApiResponse apiResponse;
// Asynchronous Call
Call<String> call = RestClient.get().getStringResponse(url, method, params);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(@NotNull Call<String> call, @NotNull Response<String> response) {
if (response.isSuccessful()) {
apiResponse = new ApiResponse(response.code(), response.body());
}
}
@Override
public void onFailure(@NotNull Call<String> call, @NotNull Throwable t) {
apiResponse = new ApiResponse();
}
});
return apiResponse;
}
我无法更改网络委托接口。我必须覆盖它,我需要使用改造异步。
非常感谢您的反馈。谢谢大家。
【问题讨论】:
-
不使用aar库接口,直接在你的应用中使用Retrofit异步调用可以吗?
标签: android asynchronous interface retrofit rx-java