如何将动态模型类设置为Retrofit请求?

时间:2018-06-14 12:37:11

标签: java android retrofit2

在我的应用程序中,我想对请求使用 Retrofit2 库,我想要设置动态响应模型,我不希望将模型设置为interface模型!
例如: 我的界面方法是:

@GET("api/server?mode=supports")
Call<SupportListResponse> getSupport_List(@Header("jwt") String token);

我不想将SupportListResponse设置为Call<>,我希望从Activity / Fragment类设置此模型动态

如何设置一般模型而不是 SupportListResponse

我的活动完整代码:

public class TimerRecyclerActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ProgressBar timerProgressBar;
    private List<Today> model = new ArrayList<>();
    private Adapter adapter;
    private ApiInterface api;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_recycler);

        api = ApiClient.getClient().create(ApiInterface.class);
        adapter = new Adapter(getApplicationContext(), model);
        adapter.setHasStableIds(true);
        recyclerView = findViewById(R.id.timerRecyclerView);
        timerProgressBar = findViewById(R.id.timerProgressBar);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        Call<AuctionsListResponse> call = api.getMainAuctions("", 1, 10);
        call.enqueue(new Callback<AuctionsListResponse>() {
            @Override
            public void onResponse(Call<AuctionsListResponse> call, Response<AuctionsListResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body().getRes() != null) {
                        if (response.body().getRes().getToday().size() > 0) {
                            timerProgressBar.setVisibility(View.GONE);
                            model.clear();
                            model.addAll(response.body().getRes().getToday());
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            }

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

            }
        });
    }
}

我怎么能?

1 个答案:

答案 0 :(得分:0)

在代码中进行一些修改

   @GET("api/server?mode=supports")
    Call<ResponseBody> getSupport_List(@Header("jwt") String token);

获得改造对象

   public static Retrofit getWorkbook() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
主要活动改造中的

    private void gettingResponseFromRetrofit() {
            ApiInterface api = ApiClient.getWhatsNew().create(ApiInterface.class);
            Call<ResponseBody> call = api.getSupport_List("", 1, 10);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                    if (response.isSuccessful()) {
                        try {
                            // here you will get the response
                            // do your stuff
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "Some Error Occur", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(@NonNull Call<ResponseBody> call, Throwable t) {
                    t.printStackTrace();
                }
            });
        }