您的位置:首页 > 理论基础 > 计算机网络

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请求了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐