android - api调用时多个视图中的多个进度条
解决方案
在你的项目中添加这个接口......
public interface RetrofitCallback {
<T> void getSuccessCallBack(Response<T> response, int position);
void getErrorCallBack(String message, int position);
}
在您的 Utility 或 RetrofitUtility 中添加这些方法。
public static <T> void callRetrofit(final Context context, final Fragment fragment, Call<T> call, final int position, final RetrofitCallback callback) {
final ProgressDialog pDialog = CommonMethod.showProgressDialog(context, context.getResources().getString(R.string.loading));// progress for whole application
call.enqueue(new Callback<T>() {
@Override
public void onResponse(Call<T> call, Response<T> response) {
pDialog.dismiss();
if (response.isSuccessful()) {
ResultBody result = (ResultBody) response.body();
if (result.isSuccess())
callback.getSuccessCallBack(response, position);
else
callback.getErrorCallBack(result.getMessage(), position);
} else
Toast.makeText(context, response.message(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<T> call, Throwable t) {
pDialog.dismiss();
if (CommonMethod.checkconnection(context)) { // checking internet connection
Toast.makeText(context, "Server_error", Toast.LENGTH_SHORT).show();
} else {
CommonMethod.showconnectiondialog(context, fragment);
}
}
});
}
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("XXXXXXXXXXXXX")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
所有改造 API
public interface RetrofitApi {
@POST("allData")
Call<UserData> getAllData();
}
RetrofitCallback
在你的 Activity 和 Fragment 中实现
像这样调用 Api
Call<UserData> call = ApiClient.getClient().create(RetrofitApi.class).getAllData();
ApiClient.callRetrofit(context, this, call, 0, this);
您将从下面的 RetrofitCallback 覆盖方法中获取数据
@Override
public <T> void getSuccessCallBack(Response<T> response, int position) {
UserData userData = ((UserData) response.body());
// do your operation over here
}
@Override
public void getErrorCallBack(String message, int position) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
这是所有模型文件....
ResultBody.class
public class ResultBody {
private String message;
private boolean success;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}
用户数据类
public class UserData extends ResultBody {
private User userData;
public User getUserData() {
return userData;
}
public void setUserData(User user) {
this.userData = user;
}
}
让我知道你是否卡在任何地方......
相关帖子或问题
推荐阅读
- angular - 如何在 Angular 10 中使用 ngforage
- firebase - 长列表的 Flutter 应用程序性能问题
- quarkus - 使用 Quarkus 运行 Specs2
- video - 如何仅在不兼容的浏览器中播放 MP4 HEVC (h265) 视频文件时检测音频?
- python - 安装扩展 plotly jupyter lab 后出现 PermissionError
- c# - AutoCAD 2021 .NET API:获取数据库中的所有块引用?
- python - 我需要欺骗获取请求的 IP
- java - 如何从 Firebase 实时数据库中的两个不同子项获取订单数据?
- javascript - json_encode 适用于整数而不适用于字符串
- r - 将表格放入 r 笔记本中的 pdf 文档中