Skip to content

Commit

Permalink
修改设置重定向方法名,重命名JsonFieldUtils为JsonObjUtils,重命名ParameterUtils为RequestBo…
Browse files Browse the repository at this point in the history
…dyUtils,添加替换一组请求的BaseUrl详见RequestSetting
  • Loading branch information
goweii committed Jan 25, 2019
1 parent 812c6da commit 78d3e2d
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ RxRequest的配置参数

- 默认的BaseUrl

- #### Map<String, String> getMultiBaseUrl()
- #### Map<String, String> getRedirectBaseUrl()

其他用于重定向的BaseUrlMapKey值为添加重定向HeaderValue值,MapValue值为BaseUrl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String getBaseUrl() {
}

@Override
public Map<String, String> getMultiBaseUrl() {
public Map<String, String> getRedirectBaseUrl() {
Map<String, String> urls = new HashMap<>(2);
urls.put(FreeApi.Config.BASE_URL_OTHER_NAME, FreeApi.Config.BASE_URL_OTHER);
urls.put(FreeApi.Config.BASE_URL_ERROR_NAME, FreeApi.Config.BASE_URL_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion rxhttp/src/main/java/per/goweii/rxhttp/request/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Api {
public interface Header {
/**
* 添加以这个为名的Header可以让这个Request使用另一个BaseUrl
* {@link RequestSetting#getMultiBaseUrl()}
* {@link RequestSetting#getRedirectBaseUrl()}
*/
String BASE_URL_REDIRECT = "RxHttp-BaseUrl-Redirect";
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package per.goweii.rxhttp.request;

import android.text.TextUtils;

import com.google.gson.Gson;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import per.goweii.rxhttp.BuildConfig;
import per.goweii.rxhttp.core.RxHttp;
import per.goweii.rxhttp.core.manager.BaseClientManager;
import per.goweii.rxhttp.core.utils.BaseUrlUtils;
Expand All @@ -32,6 +35,7 @@ class RequestClientManager extends BaseClientManager {

private static RequestClientManager INSTANCE = null;
private final Retrofit mRetrofit;
private Map<Class<?>, Retrofit> mRetrofitMap = null;

private RequestClientManager() {
mRetrofit = create();
Expand Down Expand Up @@ -61,17 +65,64 @@ private static RequestClientManager getInstance() {
* @return Api接口实例
*/
static <T> T getService(Class<T> clazz) {
return getInstance().mRetrofit.create(clazz);
return getInstance().getRetrofit(clazz).create(clazz);
}

private Retrofit getRetrofit(Class<?> clazz) {
if (clazz == null) {
return mRetrofit;
}
Retrofit retrofit = null;
if (mRetrofitMap != null && mRetrofitMap.size() > 0) {
Iterator<Map.Entry<Class<?>, Retrofit>> iterator = mRetrofitMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Class<?>, Retrofit> entry = iterator.next();
if (TextUtils.equals(entry.getKey().getName(), clazz.getName())) {
retrofit = entry.getValue();
if (retrofit == null) {
iterator.remove();
}
break;
}
}
}
if (retrofit != null) {
return retrofit;
}
Map<Class<?>, String> baseUrlMap = RxHttp.getRequestSetting().getServiceBaseUrl();
if (baseUrlMap == null || baseUrlMap.size() == 0) {
return mRetrofit;
}
String baseUrl = null;
for (Map.Entry<Class<?>, String> entry : baseUrlMap.entrySet()) {
if (TextUtils.equals(entry.getKey().getName(), clazz.getName())) {
baseUrl = entry.getValue();
break;
}
}
if (baseUrl == null) {
return mRetrofit;
}
retrofit = create(baseUrl);
mRetrofitMap.put(clazz, retrofit);
return retrofit;
}

/**
* 创建Retrofit实例
*/
@Override
protected Retrofit create() {
return create(RxHttp.getRequestSetting().getBaseUrl());
}

/**
* 创建Retrofit实例
*/
private Retrofit create(String baseUrl) {
return new Retrofit.Builder()
.client(createOkHttpClient())
.baseUrl(BaseUrlUtils.checkBaseUrl(RxHttp.getRequestSetting().getBaseUrl()))
.baseUrl(BaseUrlUtils.checkBaseUrl(baseUrl))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
Expand All @@ -85,7 +136,7 @@ protected Retrofit create() {
private OkHttpClient createOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 设置调试模式打印日志
if (BuildConfig.DEBUG) {
if (RxHttp.getRequestSetting().isDebug()) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(logging);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class BaseUrlRedirectInterceptor implements Interceptor {

public static void addTo(@NonNull OkHttpClient.Builder builder) {
Map<String, String> urls = RxHttp.getRequestSetting().getMultiBaseUrl();
Map<String, String> urls = RxHttp.getRequestSetting().getRedirectBaseUrl();
if (NonNullUtils.check(urls)) {
builder.addInterceptor(new BaseUrlRedirectInterceptor());
}
Expand All @@ -39,7 +39,7 @@ private BaseUrlRedirectInterceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Map<String, String> urls = RxHttp.getRequestSetting().getMultiBaseUrl();
Map<String, String> urls = RxHttp.getRequestSetting().getRedirectBaseUrl();
if (!NonNullUtils.check(urls)) {
return chain.proceed(original);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public abstract class DefaultRequestSetting implements RequestSetting {

@Nullable
@Override
public Map<String, String> getMultiBaseUrl() {
public Map<String, String> getRedirectBaseUrl() {
return null;
}

@Nullable
@Override
public int getSuccessCode() {
return 200;
public Map<Class<?>, String> getServiceBaseUrl() {
return null;
}

@Override
Expand Down Expand Up @@ -97,4 +98,9 @@ public Interceptor[] getNetworkInterceptors() {
@Override
public void setOkHttpClient(OkHttpClient.Builder builder) {
}

@Override
public boolean isDebug() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,31 @@
*/
public interface RequestSetting {

/**
* 设置默认BaseUrl
*/
@NonNull
String getBaseUrl();

/**
* 用于对不同的请求设置不同的BaseUrl
* 需要配合Retrofit的@Headers注解使用
* 如:@Headers({RxHttp.BASE_URL_REDIRECT + ":" + 别名})
* @return Map<别名, url>
*
* @return Map 别名,BaseUrl
*/
@Nullable
Map<String, String> getRedirectBaseUrl();

/**
* 用于对一组接口设置BaseUrl
* 这种设置方法对资源占用较大,实现方式为每组的请求创建不同的Retrofit和OkHttpClient实例,设置均相同,及下面的设置
* 建议在少数请求需要单独设置BaseUrl时使用{@link #getRedirectBaseUrl()}
*
* @return Map 接口类,BaseUrl
*/
@Nullable
Map<String, String> getMultiBaseUrl();
Map<Class<?>, String> getServiceBaseUrl();

int getSuccessCode();

Expand All @@ -43,28 +57,34 @@ public interface RequestSetting {

/**
* 获取Connect超时时长,单位为毫秒数
* 返回0则去getTimeout
* 返回0则取getTimeout
*/
@IntRange(from = 0)
long getConnectTimeout();

/**
* 获取Read超时时长,单位为毫秒数
* 返回0则去getTimeout
* 返回0则取getTimeout
*/
@IntRange(from = 0)
long getReadTimeout();

/**
* 获取Write超时时长,单位为毫秒数
* 返回0则去getTimeout
* 返回0则取getTimeout
*/
@IntRange(from = 0)
long getWriteTimeout();

/**
* 获取网络缓存的文件夹名
*/
@NonNull
String getCacheDirName();

/**
* 获取网络缓存的最大值
*/
@IntRange(from = 1)
long getCacheSize();

Expand All @@ -85,4 +105,6 @@ public interface RequestSetting {

void setOkHttpClient(OkHttpClient.Builder builder);

boolean isDebug();

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
* @author Cuizhen
* @date 2018/10/8
*/
public class JsonFieldUtils {
public class JsonObjUtils {

private final JSONObject mJsonObject;

private JsonFieldUtils() {
private JsonObjUtils() {
mJsonObject = new JSONObject();
}

public static JsonFieldUtils newInstance() {
return new JsonFieldUtils();
public static JsonObjUtils create() {
return new JsonObjUtils();
}

public JsonFieldUtils add(String key, int value) {
public JsonObjUtils add(String key, int value) {
try {
mJsonObject.put(key, value);
} catch (JSONException e) {
Expand All @@ -30,7 +30,7 @@ public JsonFieldUtils add(String key, int value) {
return this;
}

public JsonFieldUtils add(String key, float value) {
public JsonObjUtils add(String key, float value) {
try {
mJsonObject.put(key, value);
} catch (JSONException e) {
Expand All @@ -39,7 +39,7 @@ public JsonFieldUtils add(String key, float value) {
return this;
}

public JsonFieldUtils add(String key, double value) {
public JsonObjUtils add(String key, double value) {
try {
mJsonObject.put(key, value);
} catch (JSONException e) {
Expand All @@ -48,7 +48,7 @@ public JsonFieldUtils add(String key, double value) {
return this;
}

public JsonFieldUtils add(String key, boolean value) {
public JsonObjUtils add(String key, boolean value) {
try {
mJsonObject.put(key, value ? 1 : 0);
} catch (JSONException e) {
Expand All @@ -57,7 +57,7 @@ public JsonFieldUtils add(String key, boolean value) {
return this;
}

public JsonFieldUtils add(String key, String value) {
public JsonObjUtils add(String key, String value) {
try {
mJsonObject.put(key, value);
} catch (JSONException e) {
Expand All @@ -66,6 +66,10 @@ public JsonFieldUtils add(String key, String value) {
return this;
}

public JSONObject get() {
return mJsonObject;
}

public String toJson() {
return mJsonObject.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static boolean isConnected() {
if (connectivityManager != null) {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isConnected();
return networkInfo.isAvailable();
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
* @author Cuizhen
* @date 2018/9/4
*/
public class ParameterUtils {
public class RequestBodyUtils {

private ParameterUtils() {
private RequestBodyUtils() {
}

public static ParameterUtils.Builder builder() {
return new ParameterUtils.Builder();
public static RequestBodyUtils.Builder builder() {
return new RequestBodyUtils.Builder();
}

public static Map<String, RequestBody> create(String key, File value) {
public static <T> Map<String, RequestBody> create(String key, T value) {
return builder().add(key, value).build();
}

Expand Down

0 comments on commit 78d3e2d

Please sign in to comment.