大佬教程收集整理的这篇文章主要介绍了android – Retrofit,Generic Call type,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个以这种方式公开服务的休息Api:/ api / {typE} / {iD} 4’type’,因此返回4种Class类型.
我的问题是我似乎总是要明确命名返回的类:
Call<Type1> call = apiInterface.get.... Call<Type2> call = apiInterface.get....
等等…
所以现在我做
SuperClass object = null; switch(typE){ case TYPE1: Call<Type1> call = apiInterface.getType1(id); call.enqueue(new CallBACk....{ ....... object = response.body() } break; case TYPE2: Call<Type2> call = apiInterface.getType2(id); call.enqueue(new CallBACk....{ ....... object = response.body() } break; }
这感觉很不对劲.
你有没有办法做得更好,也许是泛型的东西?
谢谢,
public Call getCourseElement(Type type,String id){ Call call = null; switch (typE) { case TYPE1: call = apiInterface.getType1(id); break; case TYPE2: call = apiInterface.getType2(id); break; ... } return call; }
并以这种方式使用它:
Call call = api.getCourseElement(type,id); call.enqueue(new CallBACk() { @Override public void onResponse(Response response,Retrofit retrofit) { SuperClass superObj = (SuperClass) response.body() ........... } @Override public void onFailure(Throwable t) { .......... } });
我有一些未经检查的警告,我仍然有开关.
我想到的另一个解决方案是使用一个自定义的TypeConverter(来自json响应)创建一个TYPE1 / TYPE2元素并将其发送回SuperClass对象.但我不想测试json,因为它可以轻松改变.
以上是大佬教程为你收集整理的android – Retrofit,Generic Call type全部内容,希望文章能够帮你解决android – Retrofit,Generic Call type所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。