使用改造入队呼叫

时间:2018-09-06 22:03:33

标签: android retrofit

我是Android新手。我正在尝试通过jwt调用rest服务。
有更好的方法来拨打这两个电话吗?
看起来写得不好,因为该服务的调用已附加到jwt调用上,并且除非jwt令牌过期,否则不应每次都调用该服务,对吧?

提前感谢您的反馈

final GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);

        Call<Void> tokenCall = service.geToken(new AccountCredentials("admin", "password"));
        tokenCall.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Call<Void> call, Response<Void> response) {
                progressDialog.dismiss();
                token = response.headers().get("Authorization");

                Call<List<Event>> eventCall = service.getAllEvents(token);
                eventCall.enqueue(new Callback<List<Event>>() {

                    @Override
                    public void onResponse(Call<List<Event>> call, Response<List<Event>> response) {
                        progressDialog.dismiss();
                        generateDataList(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<Event>> call, Throwable t) {
                        progressDialog.dismiss();
                        Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onFailure(Call<Void> call, Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });

1 个答案:

答案 0 :(得分:0)

这是一个很好的解决方案。但是您可以通过在新方法中编写第二个调用并仅调用该方法来增强代码的设计。

更好的解决方案是使用RXJAVA,请参见

https://medium.com/@adinugroho/chaining-multiple-retrofit-call-using-rxjava-177b64c8103e

https://www.captechconsulting.com/blogs/a-mvp-approach-to-lifecycle-safe-requests-with-retrofit-20-and-rxjava

https://medium.com/@mtrax/rxandroid-2-with-retrofit-2-and-gson-3f08d4c2627d

相关问题