Android:dynamically pass model class to retrofit callback

后端 未结 10 1775
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 04:34

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

@POST
Call getDataFromServer(@Url String url, @Body HashMap ha         


        
10条回答
  •  情歌与酒
    2020-12-05 05:30

    Use standard generics, with a little bit of hacking around

    Define your interface like this

    public interface ApiCalls {
        @POST
        Call getResult getDataFromServer(@Url String url, @Body HashMap hashMap);
    }
    

    and call for creating api client use a helper method

    class HelperMethods {
        @SuppressWarnings("unchecked")
        private static  ApiCalls getClient() {
            return retrofit.create((Class>)(Class)ApiCalls.class);
        }
    }
    
    ApiCalls api = HelperMethods.getClient();
    

    But despite of the fact how many times it has been said here, I am gonna say it again... Don't do this .. You are giving up the whole type safety and contract validation that Retrofit is offering .. That is actually the most exciting thing about it..

提交回复
热议问题