Retrofit call.enqueue在Fragement和BottomNavigationView的onNavigationItemSelected方法中未给出任何响应(成功/失败)
在userProfileDetails方法中,我调用了Retrofit客户端。在运行代码时,API响应卡在call.enqueue方法中,并且onNavigationItemSelected方法中未调用onResponse或onFailure方法 相同的方法可以很好地在各自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();
}
答案 0 :(得分:2)
您的改造ApI在线程上运行。
注意:-因此,您每次都会获得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;