【问题标题】:Retrofit API not working in onNavigationItemSelected method and in Fragment改造 API 在 onNavigationItemSelected 方法和 Fragment 中不起作用
【发布时间】:2019-01-09 11:58:07
【问题描述】:

改造 call.enqueue 在 Fragment 和 BottomNavigationView 的 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();
    }

【问题讨论】:

  • 尝试重命名您的调用方法,因为我认为您与 class-model 有冲突。

标签: android android-fragments retrofit2


【解决方案1】:

您的 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;

【讨论】:

  • 没有具体错误。它绕过了 onResponse 和 onFailure 方法。 userProfileDetails();在活动中工作得非常好。
  • 尝试在 onResponse 和 onFailure 方法中都放断点并调试它。你会得到它是否会绕过?
  • 我在通过片段时这样做了,它直接从 call.enqueue(new Callback() 出来,而不是进入里面
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 2019-09-19
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多