如何在动态模型类中使用模型值来改造请求?

时间:2018-06-17 06:25:54

标签: java android retrofit2

在我的应用程序中,我想对请求使用 Retrofit2 库,我想要设置动态响应模型,我不希望将模型设置为interface模型!
我可以动态设置模型,并将服务器中的值显示为logCat。

但我不知道这个价值怎么样。

我的代码:

public class ApiClient {
    private static final String BASE_URL = "https://example.com/";
    private static Retrofit retrofit = null;


    public static Gson gson = new GsonBuilder().create();

    public static Retrofit getClient() {

        OkHttpClient.Builder client = new OkHttpClient.Builder();

        OkHttpClient client2 = client
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .client(client2)
                    .build();
        }
        return retrofit;
    }
}

public interface ApiInterface {
    @POST("api/server?mode=auctions_today")
    Call<ResponseBody> getMainAuctions(@Header("jwt") String jwt, @Query("page") int page, @Query("limit") int count);
}

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<ResponseBody> call2 = api.getMainAuctions("", 1, 10);
        call2.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse
                    (@NonNull Call<ResponseBody> call2, @NonNull Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                        timerProgressBar.setVisibility(View.GONE);
                        Log.e("responseLog", "OK : " + response.body().string());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                }
            }

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

    }
}

LogCat:

E/responseLog: OK : {"res":{"today":[{"id":20906,"product_id":2,"base_price":3550000,"offer_number":60,"bid_number":3,"start_date":"2018-06-17 10:55:03","duration":2,"end_date":null,"visit":0,"capacity":40,"input_price":0}}

但我不知道如何使用这个值 例如:我想将今天列表添加到带有model.addAll();的模型中,但我不知道我该怎么做?

我怎么样?你能救我吗?

1 个答案:

答案 0 :(得分:0)

您的格式错误Json,否则您可以使用Gson来解析模型值。