|
|
|
|
@ -1,8 +1,11 @@
|
|
|
|
|
package com.bjzc.yfdxj.utils;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
|
|
import com.bjzc.yfdxj.contans.Contans;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
@ -40,27 +43,27 @@ public class HttpUtils {
|
|
|
|
|
private HttpUtils(Context context, String url) {
|
|
|
|
|
Retrofit mRetrofit = new Retrofit.Builder()
|
|
|
|
|
.baseUrl(url)
|
|
|
|
|
.client(getOkHttpClient())
|
|
|
|
|
.client(getOkHttpClient(context))
|
|
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
|
|
|
.build();
|
|
|
|
|
api = mRetrofit.create(Api.class);
|
|
|
|
|
HttpUtils.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Api getApi() {
|
|
|
|
|
return getApi(Contans.IP);
|
|
|
|
|
public static Api getApi(Context context) {
|
|
|
|
|
return getApi(Contans.IP, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Api GETMAXIMOIP() {
|
|
|
|
|
return getApi(Contans.MAXIMOIP);
|
|
|
|
|
public static Api GETMAXIMOIP(Context context) {
|
|
|
|
|
return getApi(Contans.MAXIMOIP, context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Api getApi(String url) {
|
|
|
|
|
public static Api getApi(String url, Context context) {
|
|
|
|
|
|
|
|
|
|
Retrofit mRetrofit = new Retrofit.Builder()
|
|
|
|
|
.baseUrl(url)
|
|
|
|
|
.client(getOkHttpClient())
|
|
|
|
|
.client(getOkHttpClient(context))
|
|
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
|
|
|
.build();
|
|
|
|
|
Api api = mRetrofit.create(Api.class);
|
|
|
|
|
@ -69,10 +72,10 @@ public class HttpUtils {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static OkHttpClient getOkHttpClient() {
|
|
|
|
|
|
|
|
|
|
OkHttpClient.Builder httpClientBuilder = new OkHttpClient
|
|
|
|
|
.Builder();
|
|
|
|
|
private static OkHttpClient getOkHttpClient(Context context) {
|
|
|
|
|
HttpUtils.context = context;
|
|
|
|
|
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
|
|
|
|
|
httpClientBuilder.addNetworkInterceptor(new TokenHeaderInterceptor());
|
|
|
|
|
//OkHttp进行添加拦截器loggingInterceptor
|
|
|
|
|
httpClientBuilder.addInterceptor(new LoggingInterceptor());
|
|
|
|
|
|
|
|
|
|
@ -84,6 +87,22 @@ public class HttpUtils {
|
|
|
|
|
return httpClientBuilder.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class TokenHeaderInterceptor implements Interceptor {
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
|
public Response intercept(@NonNull Chain chain) throws IOException {
|
|
|
|
|
String tokenTemp = (String) SPUtils.get(context, Contans.LOGIN_TOKEN, "");
|
|
|
|
|
if (!TextUtils.isEmpty(tokenTemp)) {
|
|
|
|
|
String token = RSAUtils.decryptBASE64StrLocal(tokenTemp);
|
|
|
|
|
String credential = "Bearer " + token;
|
|
|
|
|
Request originalRequest = chain.request();
|
|
|
|
|
Request updateRequest = originalRequest.newBuilder().header("Authorization", credential).build();
|
|
|
|
|
return chain.proceed(updateRequest);
|
|
|
|
|
}
|
|
|
|
|
return chain.proceed(chain.request());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class LoggingInterceptor implements Interceptor {
|
|
|
|
|
@Override
|
|
|
|
|
public Response intercept(Chain chain) throws IOException {
|
|
|
|
|
|