Android:dynamically pass model class to retrofit c

2020-01-30 08:30发布

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
    }

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
}

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

Update To elaborate more:

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);

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
}

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.

10条回答
混吃等死
2楼-- · 2020-01-30 09:29

using JsonElement in Response would help:

     public interface serviceApi {
     //  @GET("userinfo")
    //  Observable<userInfo> getUserIfo();
    @GET("gmail/v1/users/me/profile")
    Observable<Response<JsonElement>> getUserProfile(@HeaderMap 
    Map<String,String> Headers);
    }


private void executeAPICall(String token) {
    HashMap<String, String> headers = new HashMap<>();
    Observable<Response<JsonElement>> observable = RetroFitInstance.getInstance().getAPI(token)
            .getUserProfile(ImmutableMap.<String, String>of("Authorization", String.format("Bearer %s", token))).observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io());

    Observer<Response<JsonElement>> observer = new Observer<Response<JsonElement>>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.d("error:", e.getMessage());
        }

        @Override
        public void onNext(Response<JsonElement> jsonElementResponse) {
            UserProfile userProfile = 
       getObject(jsonElementResponse,UserProfile.class);

            EmailTextView.setText("Email Address: " + 
            userProfile.getEmailAddress());
            EmailTextView.setText("Email Address: " + 
            userProfile.getEmailAddress());
            totalEmailsTextView.setText("Total Emails: " + userProfile.getMessagesTotal());
            totalThreadsTextView.setText("Total Threads: " + userProfil
    };
    subscription = observable.subscribe(observer);
}


private <T> T getObject(Response<JsonElement> jsonElementResponse, Class<T> 
                        t){
    return  new Gson().fromJson(jsonElementResponse.body().getAsJsonObject().toString(),t);
}
查看更多
smile是对你的礼貌
3楼-- · 2020-01-30 09:30

You need to use generics. That way, you can pass the desired type into the call.

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

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

By the way, I'm no retrofit expert (I use my own stuff mostly), but I think you are using it wrong.

查看更多
我想做一个坏孩纸
4楼-- · 2020-01-30 09:32

Android:dynamically pass model class to retrofit callback

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

1. Generics

2. Combine all POJO into one ......

Generics

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) {

            }
        });

    }

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

Combine all POJO into one

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 ....

I have two API login and user......

In Login API i have get JSON response like this ...

{ "success": True, "message": "Authentication successful"}

above JSON , POJO look like this

public class LoginResult{

    private String message;
    private boolean success;

    //constructor , getter and setter 
}

and Retrofit call look like this .....

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


                }

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

                }
            });

In User API i have get JSON response like this ...

{"name": "sushildlh", "place": "hyderabad"}

above JSON , POJO look like this

 public class UserResult{

        private String name;
        private String place;

        //constructor , getter and setter 
    }

and Retrofit call look like this .....

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


                }

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

                }
            }); 

Just combine both of above JSON response into one .....

public class Result{

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

            //constructor , getter and setter 
        }

and use Result inside Your API call ......

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


            }

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

            }
        }); 

Note:- You directly combine your 2 POJO class and accessing it. (This is very complicate if you have response very large and provide duplication if some KEY is same with different Variable type )

查看更多
放我归山
5楼-- · 2020-01-30 09:33

My approach is make a POJO called ResponseData in which you will have an attribute Object, so you have:

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

When you get the response you have to parse your response.body() to the desired class. So the pros: you just have one request, instead you have to parse the response.

查看更多
登录 后发表回答