Retrofit用Interceptor实现内外网接口自动切换访问(在内网IP访问失败的时候.访问外网接口)
2017-03-13 15:26
921 查看
Retrofit用Interceptor实现内外网接口自动切换访问(在内网IP访问失败的时候.访问外网接口)
主要项目里面需求有2个baseurl,必须要能够根据用户的网络状况进行baseurl的重新定向,查找了很多博文,发现retrofit实现interceptor做重定向的博客很少,将自己的成果分享下.1.依赖
compile 'com.google.code.gson:gson:2.4' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2' compile 'com.squareup.okhttp3:okhttp:3.4.2' compile group: 'com.squareup.okhttp3', name: 'okhttp-urlconnection', version: '3.3.0' compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' compile 'com.github.bumptech.glide:glide:3.7.0' compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
依赖方面主要就是OKHTTP3和retrofit2,其余部分并不是很重要,可以忽略.
2.具体代码
拦截器代码
import com.orhanobut.logger.Logger; import com.sia.app.dagger2.global.utils.FramePerfUtil; import java.io.IOException; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; /** * 切换内网和外网的拦截器 */ class RetryAndChangeIpInterceptor implements Interceptor { private final FramePerfUtil mFramePer; //这里的url定义不是很规范,可以的话请自己定义一个集合之类的直接通过集合来传比较合适 private static final String INNER_IP = RetrofitHelper.INNER_IP; private static final String COMMON_IP = RetrofitHelper.COMMON_IP; private final int mRetryTimes; //retrytimes 重试次数 RetryAndChangeIpInterceptor(int retryTimes) { mRetryTimes = retryTimes; mFramePer = FramePerfUtil.getFramePer(); } @Override public Response intercept(Chain chain) throws IOException { //这里做了url的判断来保存sp里面的内外网标识,访问2次成功就更改一下,成功就不进行操作 Request request = chain.request(); Response response = doRequest(chain, request); int tryCount = 0; String url = request.url().toString(); while (response == null && tryCount < mRetryTimes) { if (url.contains(INNER_IP)) { url = url.replace(INNER_IP, COMMON_IP); mFramePer.setIsInnerIP("false"); } else { url = url.replace(COMMON_IP, INNER_IP); mFramePer.setIsInnerIP("true"); } Request newRequest = request.newBuilder().url(url).build(); tryCount++; //这里在为空的response的时候进行请求 response = doRequest(chain, newRequest); } if (response == null) { throw new IOException(); } return respon 4000 se; } private Response doRequest(Chain chain, Request request) { Response response = null; try { response = chain.proceed(request); } catch (Exception e) { Logger.e(e.toString()); } return response; } }
两个ip地址为
static final String INNER_IP = "http://10.169.1.8/"; static final String COMMON_IP = "http://116.239.5.6:6217/";
定义retrofit
OkHttpClient client = new OkHttpClient.Builder() .retryOnConnectionFailure(true) .addInterceptor(new RetryAndChangeIpInterceptor(2)) .build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl) .addConverterFactory(SoapConverterFactory.create()) .client(client) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); return retrofit.create(HttpApi.class); }
具体接口部分略过
最后看下接口访问的log
这里就是完整的一个自动切换baseurl的retrofit请求了.
相关文章推荐
- 用shell脚本实现自动切换内网和外网实现高可用
- mysql远程访问,主从/主主双机备份,以及使用keeplived实现故障自动切换(一)
- ThinkPHP实现移动端访问自动切换主题模板
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- Seafile内外网同时访问的实现(端口映射:将外网的一个端口映射到内网80端口)
- Struts2.0里的过滤器interceptor之用户只可以访问Login.action与Register.action,访问其它.action的链接时,自动切换到登录页面
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- ngrok集成在本地Node.js项目服务器,实现F5调试即可自动打开浏览器且通过外网可访问本地服务器。
- 通过拦截器Interceptor实现Spring MVC中Controller接口访问信息的记录
- mysql远程访问,主从/主主双机备份,以及使用keeplived实现故障自动切换(二)
- win8安装 VisualSVN Server 服务失败解决办法——花生壳+visiualSVN实现外网访问局域网内的SVN(二)
- 手机访问PC端自动切换手机端的实现
- 用shell脚本实现自动切换内网和外网实现高可用
- 利用RSVIEW32的事件机制实现软冗余自动切换
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- Web C#2.0 DataSet和Reader封装组件实现自动多数据库切换(含组件源码和实例)
- 一步一步SharePoint 2007之十九:解决实现注册用户后,自动具备访问网站的权限的问题(1)——配置Provider
- VB实现SHELL扩展之接口参数获取失败探析