本文介绍了Android:动态传递模型类以进行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将json响应映射到pojo,通常我们会这样做

In retrofit to map json response to pojo usually we do this

@POST
Call<User> getDataFromServer(@Url String url, @Body HashMap<String,Object> hashMap);

ApiCalls api = retrofit.create(ApiCalls.class);
    Call<User> call = api.getDataFromServer(StringConstants.URL,hashMap);
    call.enqueue(new Callback<User>() {
         //Response and failure callbacks
    }

其中User是我的Pojo类.但是对于其他每个请求,我都需要制作不同的pojo并编写相同的代码进行改造调用.我想制作一个用于调用api的方法,并将相应的pojo类传递给改造调用.像这样

where User is my Pojo class.But for every other request i need to make different pojo and write same code for retrofit call.I want to make a single method for calling api and pass the respective pojo class to retrofit call. like this

ApiCalls api = retrofit.create(ApiCalls.class);
Call<*ClassPassed*> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<*ClassPassed*>() {
     //Response and failure callbacks
}

所以现在我可以将任何pojo类都转换为单个方法并获取响应.这只是为了避免一次又一次地重写相同的代码.

so now i can any pojo class to single method and get the response.This is just to avoid rewriting the same code again and again.is this possible

更新详细说明:

假设我需要提出两个请求.第一个是获取userDetails,另一个是PatientDetails.So,我必须创建两个模型类User和Patient.因此,在翻新api中,我会遇到类似的情况

Suppose I need to make two requests. First one is to get userDetails and the other is patientDetails.So i have to create two model classes User and Patient.So in retrofit api i'll be having something like this

@POST
Call<User> getDataFromServer(@Url String url, @Body HashMap<String,Object> hashMap);

@POST
Call<Patient> getDataFromServer(@Url String url, @Body HashMap<String,Object> hashMap);

在我的FragmentUser和FragmentPatient类中,我将这样做

and in my FragmentUser and FragmentPatient class i'll be doing this

  ApiCalls api = retrofit.create(ApiCalls.class);
Call<User> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<User>() {
     //Response and failure callbacks
}

ApiCalls api = retrofit.create(ApiCalls.class);
Call<Patient> call = api.getDataFromServer(StringConstants.URL,hashMap);
call.enqueue(new Callback<Patient>() {
     //Response and failure callbacks
}

但是这里的代码只是因为不同的pojo类而重新出现,我需要在其他片段中针对不同的请求重复相同的代码.因此,我需要制作一个通用方法,使其可以接受任何pojo类,然后从片段开始,我将传递要映射的pojo.

but here the code is repaeting just beacuse of different pojo classes.I need to repeat the same code in every other fragments for different requests.So i need to make a generic method where it can accept any pojo class and then from fragment i'll be just passing the pojo to be mapped.

推荐答案

您可以通过2种方法来实现此目的.........

There is 2 ways you can do this .........

1.泛型

2.将所有POJO合并为一个......

在泛型中,您必须将方法与类一起传递.请看例子.....

In the Generics you have to pass the method with the class. pleas have look on example .....

ApiCalls api = retrofit.create(ApiCalls.class);

Call<User> call = api.getDataFromServer(StringConstants.URL,hashMap);

callRetrofit(call,1);

 public static <T> void callRetrofit(Call<T> call,final int i) {

        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
            if(i==1){
                  User user = (User) response.body(); // use the user object for the other fields
             }else if (i==2){
                 Patient user = (Patient) response.body(); 
              }


            }

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

            }
        });

    }

注意:-在翻新呼叫上方,键入您的回复为YOUR OBJECT,这样您就可以访问其字段和方法

NOTE:- Above retrofit call TypeCast your response into YOUR OBJECT, so you can access its field and methods

它非常易于使用.您必须将所有POJO类合并为一个,并在Retrofit中使用它们.请看下面的例子....

It is very easy to use . You have to combine your all POJO class into one and use them inside the Retrofit. please have look on below example ....

在Login API中,我得到了这样的JSON响应...

在JSON之上,POJO如下所示

public class LoginResult{

    private String message;
    private boolean success;

    //constructor , getter and setter 
}

和翻新呼叫如下所示...

 call.enqueue(new Callback<LoginResult>() {
                @Override
                public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {


                }

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

                }
            });

在用户API中,我得到了这样的JSON响应.. .

在JSON之上,POJO如下所示

 public class UserResult{

        private String name;
        private String place;

        //constructor , getter and setter 
    }

和翻新呼叫如下所示...

 call.enqueue(new Callback<UserResult>() {
                @Override
                public void onResponse(Call<UserResult> call, Response<UserResult> response) {


                }

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

                }
            }); 

只需将以上两个JSON响应合并为一个.....

public class Result{

            private String name;
            private String place;
            private String message;
            private boolean success;

            //constructor , getter and setter 
        }

并在您的API调用中使用Result ......

  call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {


            }

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

            }
        }); 

注意:-您直接组合2 POJO类并对其进行访问. (如果您的响应非常大,并且如果某些KEY的变量类型相同,则重复操作会非常复杂).

这篇关于Android:动态传递模型类以进行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:13