Android APP okhttp网络请求框架更换多个baseUrl

主要思路是请求url时加个请求头,然后在拦截器里通过本次请求的请求头来判断使用对应的baseUrl。

主要代码


import com.xxx.xxx.MyClient;

import java.io.IOException;
import java.util.List;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class MulBaseUrlInterceptor implements Interceptor {

    public MulBaseUrlInterceptor() {

    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //获取处理后的新newRequest
        Request newRequest;
        //获取老的url
        HttpUrl oldUrl = request.url();
        //获取originalRequest的创建者builder
        Request.Builder builder = request.newBuilder();
        // 获取头信息的集合
        List<String> urlTypeList = request.headers(MyClient.URL_TYPE_KEY);
        if (urlTypeList != null && urlTypeList.size() > 0) {
            //删除原有配置中的值,就是namesAndValues集合里的值
            builder.removeHeader(MyClient.URL_TYPE_KEY);
            //获取头信息中配置的value
            String headerValueFirst = urlTypeList.get(0);
            //根据头信息中配置的value,来匹配新的base_url地址
            HttpUrl baseURL = null;
            //重建新的HttpUrl,需要重新设置的url部分
            String oldUrlStr = oldUrl.toString();
            String oldUrlHost = oldUrl.host();
            int oldUrlHostIndex = oldUrlStr.indexOf(oldUrlHost);
            int oldUrlStrLength = oldUrlHost.length();
            switch (headerValueFirst) {
                case MyClient.URL_TYPE_VALUE_MIDDLE: // 第一种
                    baseURL = HttpUrl.parse(MyClient.getMiddleUrl() + oldUrlStr.substring(oldUrlHostIndex + oldUrlStrLength+1));
                    break;
                case MyClient.URL_TYPE_VALUE_TASK: // 第二种
                    baseURL = HttpUrl.parse(MyClient.getTaskUrl() + oldUrlStr.substring(oldUrlHostIndex + oldUrlStrLength+1));
                    break;
                case MyClient.URL_TYPE_VALUE_BASE: // 第三种
                    baseURL = HttpUrl.parse(MyClient.getStorageUrl() + oldUrlStr.substring(oldUrlHostIndex + oldUrlStrLength+1));
                    break;
            }
            if (baseURL != null) {
                newRequest = builder.url(baseURL).build();
            } else {
                newRequest = request;
            }
        } else {
            newRequest = request;
        }

        Response response = chain.proceed(newRequest);

        return response;
    }
}

第二步,使用这个拦截器

  • addInterceptor(new MulBaseUrlInterceptor())即可
private static Retrofit getMyRetrofit() {
        SSLSocketFactory sslSocketFactory = getSSLSocketFactory();
        if(retrofit == null) {
            okHttpClient = sOkHttpBuilder
                    .readTimeout(60L, TimeUnit.SECONDS)// 读取超时
                    .writeTimeout(60L, TimeUnit.SECONDS)// 写入超时
                    .connectTimeout(60L, TimeUnit.SECONDS)// 连接超时
                    .addInterceptor(new XxxInterceptor())
                    .addInterceptor(new XxxInterceptor(1))
                    .addInterceptor(new MulBaseUrlInterceptor())
                    .sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0])
                    .hostnameVerifier(new HostnameVerifier() {
                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    })
                    .build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(getOperationUrl())
                    .client(okHttpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

你可能感兴趣的:(网络框架相/请求相关,APP,okhttp,retrofit,http,android,url)