且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用改造从 API 获取数据

更新时间:2023-01-06 07:43:23

使用 Android studio 插件GsonFormat"创建 ResObj.class它将为您的 APICall 创建一个完美的 POJO 类,只是通过 Postman 的 API GET 响应.它将创建一个合适的 POJO 类,您可以从中简单地调用

Create ResObj.class using Android studio plugin "GsonFormat" It will create a perfect POJO class for your APICall, just past the API GET response from Postman. It will create a suitable POJO class, from which you can simply call

 private void callApi() {
    Log.e(TAG, "callApi: inside apiCall");
    Call<List> call = 
 login.getUserDetails();

 call.enqueue(new Callback<List>() {
        @Override
        public void onResponse(Call<List> call, @NonNull Response<List>response) {
            List items = response.body();

            items = list.data;
            String user_id = items.getUserID();
        }

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

        }

在想要获取数据的地方调用 callApi() 方法;如果您复制并粘贴它,则此方法和方法将不起作用,您需要根据您的 POJO 类对其进行更改,但这将提供帮助还要添加您的布局和活动代码以提供上下文

call callApi() method where you want to get data; This method and method will not work if you copy and paste it you need to alter it according to your POJO class but this will provide help Also add your layout and activity code to give us context