Retrofit的初次使用
程序员文章站
2022-07-05 15:40:43
rxretrofitlibrary是一个已经写好的网络框架库,先以本地Module导入到自己的项目中。 1、它的初始化操作大多在自定义的application中完成,如: public class App extends Application { @Override public void onC ......
rxretrofitlibrary是一个已经写好的网络框架库,先以本地module导入到自己的项目中。
1、它的初始化操作大多在自定义的application中完成,如:
public class app extends application { @override public void oncreate() { super.oncreate(); //初始化网络框架库 rxretrofitapp.init(this,true); } }
2、在rxretrofitlibrary下定义一个回调信息统一封装类(即实际工作中网络请求的返回json实体类),泛型t表示用户真正关心的数据data类,例如:
/** * 回调信息统一封装类 */ public class baseresultentity<t> { // 判断标示 private int ret; // 提示信息 private string msg; //显示数据(用户需要关心的数据) private t data; public string getmsg() { return msg; } public void setmsg(string msg) { this.msg = msg; } public t getdata() { return data; } public void setdata(t data) { this.data = data; } public int getret() { return ret; } public void setret(int ret) { this.ret = ret; } }
3、在项目中使用gsonformat插件快速创建简单的data实体类
public class data { private string downurl; private string icouri; private string name; private string packagename; public string getdownurl() { return downurl; } public void setdownurl(string downurl) { this.downurl = downurl; } public string geticouri() { return icouri; } public void seticouri(string icouri) { this.icouri = icouri; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpackagename() { return packagename; } public void setpackagename(string packagename) { this.packagename = packagename; } @override public string tostring() { return "name:"+name+",packagename:"+packagename ; } }
4、在项目下创建相应的网络请求接口(@get注解后带的参数为网络请求url除baseurl部分,因为baseurl保存在了5中的请求同一封装类中,便于管理)
public interface httpgetservice { @get("url") observable<baseresultentity<data>> getdata(); }
5、为了使用方便,在rxretrofitlibrary下再定义一个请求统一封装类baseapi,此处实现的func1接口泛型为baseresultentity<t>, t。如果在实际项目中,是为了使用rxjava的map操作符将baseresultentity<t>转换为t。(转换的方法重点在代码最下面的call方法中,根据实际的业务逻辑来定)
/** * 请求统一封装 * @param <t> */ public abstract class baseapi<t> implements func1<baseresultentity<t>, t>{ //rx生命周期管理 private softreference<rxappcompatactivity> rxappcompatactivity; /*回调*/ private softreference<httponnextlistener> listener; /*是否能取消加载框*/ private boolean cancel; /*是否显示加载框*/ private boolean showprogress; /*是否需要缓存处理*/ private boolean cache; /*基础url*/ private string baseurl = "http://192.168.0.101:8080/"; /*方法-如果需要缓存必须设置这个参数;不需要不用設置*/ private string mothed; /*超时时间-默认6秒*/ private int connectiontime = 6; /*有网情况下的本地缓存时间默认60秒*/ private int cookienetworktime = 60; /*无网络的情况下本地缓存时间默认30天*/ private int cookienonetworktime = 24 * 60 * 60 * 30; /* 失败后retry次数*/ private int retrycount = 1; /*失败后retry延迟*/ private long retrydelay = 100; /*失败后retry叠加延迟*/ private long retryincreasedelay = 10; public baseapi(httponnextlistener listener, rxappcompatactivity rxappcompatactivity) { setlistener(listener); setrxappcompatactivity(rxappcompatactivity); setshowprogress(false); setcache(false); setcancel(true); setcookienetworktime(60); setcookienonetworktime(24*60*60); } /** * 设置参数 * * @param retrofit * @return */ public abstract observable getobservable(retrofit retrofit); public int getcookienonetworktime() { return cookienonetworktime; } public void setcookienonetworktime(int cookienonetworktime) { this.cookienonetworktime = cookienonetworktime; } public int getcookienetworktime() { return cookienetworktime; } public void setcookienetworktime(int cookienetworktime) { this.cookienetworktime = cookienetworktime; } public string getmothed() { return mothed; } public int getconnectiontime() { return connectiontime; } public void setconnectiontime(int connectiontime) { this.connectiontime = connectiontime; } public void setmothed(string mothed) { this.mothed = mothed; } public string getbaseurl() { return baseurl; } public void setbaseurl(string baseurl) { this.baseurl = baseurl; } public string geturl() { return baseurl + mothed; } public void setrxappcompatactivity(rxappcompatactivity rxappcompatactivity) { this.rxappcompatactivity = new softreference(rxappcompatactivity); } public boolean iscache() { return cache; } public void setcache(boolean cache) { this.cache = cache; } public boolean isshowprogress() { return showprogress; } public void setshowprogress(boolean showprogress) { this.showprogress = showprogress; } public boolean iscancel() { return cancel; } public void setcancel(boolean cancel) { this.cancel = cancel; } public softreference<httponnextlistener> getlistener() { return listener; } public void setlistener(httponnextlistener listener) { this.listener = new softreference(listener); } public int getretrycount() { return retrycount; } public void setretrycount(int retrycount) { this.retrycount = retrycount; } public long getretrydelay() { return retrydelay; } public void setretrydelay(long retrydelay) { this.retrydelay = retrydelay; } public long getretryincreasedelay() { return retryincreasedelay; } public void setretryincreasedelay(long retryincreasedelay) { this.retryincreasedelay = retryincreasedelay; } /* * 获取当前rx生命周期 * @return */ public rxappcompatactivity getrxappcompatactivity() { return rxappcompatactivity.get(); } @override public t call(baseresultentity<t> httpresult) { //map 定义转换规则 if (httpresult.getret() == 0) {//0失败,1成功 throw new httptimeexception(httpresult.getmsg()); } return httpresult.getdata(); } }
6、在项目下创建子类dataapi来继承5中的请求统一封装类
public class dataapi extends baseapi<data> { public dataapi(httponnextlistener listener, rxappcompatactivity rxappcompatactivity) { super(listener, rxappcompatactivity); //允许缓存 setcache(true); //缓存的标志 setmothed("appstore/test"); } @override public observable getobservable(retrofit retrofit) { httpgetservice httpgetservice = retrofit.create(httpgetservice.class); return httpgetservice.getdata(); } }
7、最终在继承自rxappcompatactivity(因为rxjava需要控制生命周期)的子activity中调用来得到网络请求数据
dataapi api = new dataapi(new httponnextlistener<data>() { @override public void onnext(data data) { log.i("mainactivity","onnext:"+data.tostring()) ; log.i("mainactivity","downurl:"+data.getdownurl()); } @override public void oncachenext(string string) { super.oncachenext(string); log.i("mainactivity","oncache:"+string); } @override public void onerror(throwable e) { super.onerror(e); } },this); httpmanager manager = httpmanager.getinstance(); //执行网络请求 manager.dohttpdeal(api);