【问题标题】:How to set dynamically model class to Retrofit requests?如何将动态模型类设置为改造请求?
【发布时间】:2018-11-24 05:54:27
【问题描述】:

在我的应用程序中,我想使用 Retrofit2 库来处理请求,并且我想设置 动态 响应模型,我不想将模型设置为 interface 模型!
例如: 我的接口方法是:

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

我不想将 SupportListResponse 设置为 Call&lt;&gt; ,我想从 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) {

            }
        });
    }
}

怎么办?

【问题讨论】:

  • 你想只得到json响应,这样你就可以根据你的要求制作模型类了吗?
  • 以字符串形式获取响应,然后根据响应中的某个值将其解析为适当的模型类。
  • @007,是的,我的兄弟。你能帮助我吗?请
  • 泛型等价物将是 (declaration) Call getSupport_List(@Header("jwt") String token);所以你可以通过使用 Call call = calls.getSupport)List("") 来推断,但我认为改造使用 aapt 来生成代码,所以你的问题不在于打字

标签: java android retrofit2


【解决方案1】:

对你的代码做一些修改

   @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();
                }
            });
        }

【讨论】:

  • 谢谢,我想将 SupportListResponse 设置为 Activity 代码。如:Call&lt;AuctionsListResponse&gt; call = api.getMainAuctions("", 1, 10);。我该怎么办?
  • response.body().string();您可以将您的响应转换为字符串。然后根据你的要求解析json
  • 我将代码写入活动:Call&lt;AuctionsListResponse&gt; call = api.getMainAuctions("", 1, 10);。但是当使用您的代码时,会显示此代码的错误:Call&lt;AuctionsListResponse&gt; call = api.getMainAuctions("", 1, 10);
  • 我该怎么办?你能帮帮我吗?
  • 感谢您更新您的问题,亲爱的兄弟,但我更新了我的问题并添加了活动完整代码。你能看到它并帮助我处理上面的代码吗?请。因为我是业余爱好者,真的需要你的帮助。请 。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 2019-06-14
  • 2011-03-26
相关资源
最近更新 更多