你真的会用Gson吗?Gson使用指南 https://blog.csdn.net/wsgqp/article/details/51995745
当后台返回的数据中带有 为空的值的时候例如 age=
{"data":{"exist":2,"AppEffectSoundSort":""},"message":"ok","status":2000000}
AppEffectSoundSort 这个对象本来应该是list 可是返回一个 “” ,正常的返回结果应该是
BaseBean{status=2000000, message='ok', data=DiyClound{exist=2, AppEffectSoundSort=[]}}
gson在解析的时候就会报错,
特别是在本来要返回list的时候返回为空,如果用gson直接转化为bean就会报错。
所以需要在okttp中处理,要弄清楚原理请认真阅读上面(你真的会用Gson吗?Gson使用指南 https://blog.csdn.net/wsgqp/article/details/51995745)这篇文章:
主要的思想就是利用registerTypeAdapter 和 JsonSerializer与JsonDeserializer 在处理json的时候对返回的数据进行判断处理。
下面直接贴代码:
okhttp中: GsonConverterFactory.create(buildGson())
//响应后
okClient.addInterceptor(new ResponseInterceptor());
retrofit = new Retrofit.Builder()
.baseUrl(URL_BASE)
.client(okClient.build())
.addConverterFactory(GsonConverterFactory.create(buildGson()))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
server = retrofit.create(RequestServer.class);
}
public Gson buildGson() {
if (gson == null) {
gson = new GsonBuilder()
.registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
.registerTypeAdapter(int.class, new IntegerDefault0Adapter())
.registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
.registerTypeAdapter(double.class, new DoubleDefault0Adapter())
.registerTypeAdapter(Long.class, new LongDefault0Adapter())
.registerTypeAdapter(long.class, new LongDefault0Adapter())
.registerTypeHierarchyAdapter(List.class, new ListJsonDeserializer())
.create();
}
return gson;
}
IntegerDefault0Adapter.java
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import java.lang.reflect.Type;
/**
* created by Administrator
* on 2020/6/19
*/
public class IntegerDefault0Adapter implements JsonSerializer, JsonDeserializer {
@Override
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
try {
if (json.getAsString().equals("") || json.getAsString().equals("null")) {
//定义为int类型,如果后台返回""或者null,则返回0
return 0;
}
} catch (Exception ignore) {
}
try {
return json.getAsInt();
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
}
@Override
public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src);
}
}
LongDefault0Adapter.java
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import java.lang.reflect.Type;
/**
* created by Administrator
* on 2020/6/19
*/
public class LongDefault0Adapter implements JsonSerializer, JsonDeserializer {
@Override
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
if (json.getAsString().equals("") || json.getAsString().equals("null")) {
//定义为long类型,如果后台返回""或者null,则返回0
return Long.valueOf(0);
}
} catch (Exception ignore) {
}
try {
return json.getAsLong();
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
}
@Override
public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src);
}
}
DoubleDefault0Adapter.java
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import java.lang.reflect.Type;
/**
* created by Administrator
* on 2020/6/19
*/
public class DoubleDefault0Adapter implements JsonSerializer, JsonDeserializer {
@Override
public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
if (json.getAsString().equals("") || json.getAsString().equals("null")) {
//定义为double类型,如果后台返回""或者null,则返回0.00
return 0.00;
}
} catch (Exception ignore) {
}
try {
return json.getAsDouble();
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
}
@Override
public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src);
}
}
ListJsonDeserializer.java
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* created by Administrator
* on 2020/6/19
*/
public class ListJsonDeserializer implements JsonDeserializer> {
@Override
public List> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json.isJsonArray()) {
JsonArray array = json.getAsJsonArray();
Type itemType = ((ParameterizedType) typeOfT).getActualTypeArguments()[0];
List list = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
JsonElement element = array.get(i);
Object item = context.deserialize(element, itemType);
list.add(item);
}
return list;
} else {
//和接口类型不符,返回空List
return Collections.EMPTY_LIST;
}
}
}