Skip to content

Commit

Permalink
新增Gson自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
cuizhen committed May 10, 2019
1 parent 75e1d97 commit 2c6ffb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ protected Retrofit create() {
* 创建Retrofit实例
*/
private Retrofit create(String baseUrl) {
return new Retrofit.Builder()
Retrofit.Builder builder = new Retrofit.Builder()
.client(createOkHttpClient())
.baseUrl(BaseUrlUtils.checkBaseUrl(baseUrl))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.build();
.baseUrl(BaseUrlUtils.checkBaseUrl(baseUrl));
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
Gson gson = RxHttp.getRequestSetting().getGson();
if (gson == null) {
gson = new Gson();
}
builder.addConverterFactory(GsonConverterFactory.create(gson));
return builder.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.Gson;

import java.util.Map;

import okhttp3.Interceptor;
Expand Down Expand Up @@ -121,6 +123,12 @@ public boolean enableTls12BelowAndroidKitkat() {
public void setOkHttpClient(OkHttpClient.Builder builder) {
}

@Nullable
@Override
public Gson getGson() {
return null;
}

@Override
public boolean isDebug() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.Gson;

import java.util.Map;

import okhttp3.Interceptor;
Expand Down Expand Up @@ -128,6 +130,12 @@ public interface RequestSetting {
*/
void setOkHttpClient(OkHttpClient.Builder builder);

/**
* 在创建OkHttpClient之前调用,及框架完成所有配置后
*/
@Nullable
Gson getGson();

/**
* 是否打开调试模式
*/
Expand Down

0 comments on commit 2c6ffb4

Please sign in to comment.