首页 > 解决方案 > 改造 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();
    }

标签: androidandroid-fragmentsretrofit2

解决方案


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

推荐阅读