android - 改造 API 在 onNavigationItemSelected 方法和 Fragment 中不起作用
问题描述
改造 call.enqueue 在 Fragment 和底部导航视图的 onNavigationItemSelected 方法中没有给出任何响应(成功/失败)
在 userProfileDetails 方法中,我调用了 Retrofit 客户端。运行代码时,API 响应卡在 call.enqueue 方法中,并且 onResponse 或 onFailure 方法未在 onNavigationItemSelected 方法中调用相同的方法在各个 Activity 的 onCreate 方法中运行良好。UserProfileFragment 类中也发生了同样的问题。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()){
case R.id.menu_landingorder:
fragment = new OrderFragment();
break;
case R.id.menu_reccuringcart:
fragment = new RecurringCartFragment();
break;
case R.id.menu_landingprofile:
userProfile =userProfileDetails();
fragment = new UserProfileFragment();
Bundle b = new Bundle();
b.putSerializable(StringConstants.USERPROFILE, userProfile);
fragment.setArguments(b);
break;
}
if(fragment!=null){
displayFragment(fragment);
}
return false;
}
private UserProfile userProfileDetails() {
Call<UserProfile> call = RetrofitClient.getInstance().getApi().getUserDetails("Bearer "+ user.get(SessionManager.KEY_SESSION_TOKEN));
Log.i("user :: " , "Bearer "+ user.get(SessionManager.KEY_SESSION_TOKEN));
call.enqueue(new Callback<UserProfile>(){
@Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if(response.isSuccessful()){
userProfile = response.body();
Log.i("Response SuccessFul :: ", userProfile.getPhoneNo());
}else {
userProfile = response.body();
Log.i("Response Failure :: ", userProfile.getPhoneNo());
try {
Log.e("Coming Inside", String.valueOf(response.code()) + " Err Body " + response.errorBody());
Log.i("user :: ", userProfile.getPhoneNo());
} catch (Exception e) {
Log.e("Error Inside", String.valueOf(response.code()) + " Err Body " + response.errorBody());
}
}
}
@Override
public void onFailure(Call<UserProfile> call, Throwable t) {
Log.e("Issues ", t.getMessage() );
}
});
return userProfile;
}
private void displayFragment(Fragment fragment){
getSupportFragmentManager().beginTransaction()
.replace(R.id.landingrelativeLayout, fragment)
.commit();
Toast.makeText(LandingActivity.this, "Display Fragment Home", Toast.LENGTH_LONG).show();
}
解决方案
您的 Retrofit ApI 在 Thread 上运行。
注意:- 所以你每次都会得到 userProfile
null
。因为该方法在您的 api 执行之前返回。
你们都在onResponse()
方法内部工作并使其签名为void
.
private void userProfileDetails() {
Call<UserProfile> call = RetrofitClient.getInstance().getApi().getUserDetails("Bearer "+ user.get(SessionManager.KEY_SESSION_TOKEN));
Log.i("user :: " , "Bearer "+ user.get(SessionManager.KEY_SESSION_TOKEN));
call.enqueue(new Callback<UserProfile>(){
@Override
public void onResponse(Call<UserProfile> call, Response<UserProfile> response) {
if(response.isSuccessful()){
userProfile = response.body();
Log.i("Response SuccessFul :: ", userProfile.getPhoneNo());
// Do your post operation here or after this
fragment = new UserProfileFragment();
Bundle b = new Bundle();
b.putSerializable(StringConstants.USERPROFILE, userProfile);
fragment.setArguments(b);
getSupportFragmentManager().beginTransaction()
.replace(R.id.landingrelativeLayout, fragment)
.commit();
}else {
userProfile = response.body();
Log.i("Response Failure :: ", userProfile.getPhoneNo());
try {
Log.e("Coming Inside", String.valueOf(response.code()) + " Err Body " + response.errorBody());
Log.i("user :: ", userProfile.getPhoneNo());
} catch (Exception e) {
Log.e("Error Inside", String.valueOf(response.code()) + " Err Body " + response.errorBody());
}
}
}
@Override
public void onFailure(Call<UserProfile> call, Throwable t) {
Log.e("Issues ", t.getMessage() );
}
});
}
像这样调用你的方法......
case R.id.menu_landingprofile:
userProfileDetails();
break;
推荐阅读
- arrays - 数组中所有元素的最小总和称为数组成本
- retrofit - 失眠比改造提供更长的字符串
- javascript - 在做任何其他事情之前等待 axios 响应
- mysql - 如何备份存储在外部存储设备上的 mysql 数据库?
- swift - 从 JSON 初始化 `Package.swift`
- javascript - 反应中的表达cors错误找不到财产
- selenium - 如何通过 Safari 选项禁用 hub.crossbrowsertesting.com(RobotFramework)中的弹出窗口?
- angular - 通过docker容器调用rest API
- c# - 字节 [] 和图像的 SQL 更新查询不适用于 C# 应用程序
- node.js - 将大数写入 JSON 文件