使用2个api请求并在一个recyclerview中显示数据

yuvru6vn  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(186)

我使用改型2来获取数据,但在我的api响应中,我得到的是字段user\u id,而不是user\u name,因此当我将数据显示到回收器视图中时,我得到的结果如下:在此处输入图像描述
但是我需要显示用户名,我需要使用另一个api请求来获取用户名(选项1:使用请求来获取所有用户及其id的列表,或者选项2使用@path中具有用户id的请求来获取该特定id的用户名)。
我尝试了一些方法来实现这一点,但没有任何效果。这是我最后一次尝试
这是我在recycler视图中显示数据的代码:

private void displayfollowup(String id,String sestoken) {
    followuplist=new ArrayList<>();

    Retrofit retrofit = RetrofitInstance.getRetrofitInstance();
    final Api api = retrofit.create(Api.class);
    Call<List<TraitementTicketModel>> call = api.getfollowup(id, sestoken);
    call.enqueue(new  Callback <List<TraitementTicketModel>>() {

        @Override
        public void onResponse(Call<List<TraitementTicketModel>> call, Response<List<TraitementTicketModel>> response) {
            if (!response.isSuccessful()) {
                Toast.makeText(getApplicationContext(), "Something is wrong !! ", Toast.LENGTH_LONG).show();
                Log.e("TAG", "onResponse: something is wrong");

            } else if (response.body() == null) {

                return;
            }

            List<TraitementTicketModel> followups = response.body();

            for (TraitementTicketModel followup : followups) {

                followuplist.add(followup);

            }

            followuplist.add(firstfollowup());
            PutDataIntoRecyclerView(followuplist);

        }

        @Override
        public void onFailure(Call<List<TraitementTicketModel>> call, Throwable t) {
            Toast.makeText(getApplicationContext(),"Pas de connextion internet",Toast.LENGTH_LONG).show();
        }
    });

}

这是我的尝试,我将此代码添加到displayfollowup方法中,尝试使用另一个api请求getusername将user_id设置为user_名称,但应用程序不断崩溃:

for (TraitementTicketModel followup : followups) {

               getUsername(followup.getUsers_id(), new usercallback() {
                   @Override
                   public void onSuccess(@NonNull User value) {
                       followup.setUsers_id(value.getName());
                   }

                   @Override
                   public void onError(@NonNull Throwable throwable) {

                   }
               });
                followuplist.add(followup);

            }

我使此接口能够使用getusername响应数据:

public interface usercallback {
    void onSuccess(@NonNull User value);

    void onError(@NonNull Throwable throwable);
}

这是获取用户名的另一个api请求:

private void getUsername(String userid,@Nullable usercallback callbacks) {
    SharedPreferences sp =getApplicationContext().getSharedPreferences("tokenPref", Context.MODE_PRIVATE);
    String sestoken = sp.getString("token","");

    Retrofit retrofit= RetrofitInstance.getRetrofitInstance();
    final Api api= retrofit.create(Api.class);
    Call<User> call = api.getUsername(userid,sestoken);
    call.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            if (!response.isSuccessful()) {
                Toast.makeText(getApplicationContext(),"Something is wrong !! ",Toast.LENGTH_LONG).show();
                Log.e("TAG", "onResponse: something is wrong");

            } else if (response.body()==null) {
                return;
            }

                callbacks.onSuccess(response.body());

        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {

                callbacks.onError(t);

        }
    });
}

我真的被困在这里了,我不能显示用户名而不是他的id,正如你所看到的,非常感谢你的帮助

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题