最近在做后台数据加密,然后遇到这样一个问题,服务器的数据全部用加密算法加密了,发现无法使用“com.squareup.retrofit2:converter-gson:2.1.0”Jar包了。
自定义ConverterFactory
现在安卓流行Retrofit+Okhttp+Converter-gson,不过如果数据如果加密了,使用Converter-gson进行解析的时候会出现问题。字符串被加密了,自然不能直接使用Gson进行解析,这样我们就需要自己写一个Converter架包了。
其中Retrofit.Builder中有一个addConverterFactory,我们最后要将自己的ConverterFactory作为参数写入,通过这个ConverterFactory接口,实现我们自己的解析类即可。
//添加ConverterFactory
Retrofit client = new Retrofit.Builder() .addConverterFactory(ConverterAesJsonFactory.create())
ConverterFactory
要实现这个ConverterFactory需要写着三部分代码:
public class ConverterAesJsonFactory extends Converter.Factory {
private final Gson gson;
public static ConverterAesJsonFactory create() {
return create(new Gson());
}
public static ConverterAesJsonFactory create(Gson gson) {
return new ConverterAesJsonFactory(gson);
}
private ConverterAesJsonFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type,
Annotation[] annotations,
Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new AesGsonResponseBodyConverter(gson, adapter);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations,
Annotation[] methodAnnotations,
Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new AesJsonRequestBodyConverter<>(gson, adapter);
}
}
ResponseBodyConverter
这个类也就是要进行解密操作的那个类了,除了解密算法,还有Json解析的代码。
/**
* 响应体
* Created by ChenSS on 2016/12/26.
*/
final class AesGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final Gson gson;
private final TypeAdapter<T> adapter;
AesGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override
public T convert(ResponseBody responseBody) throws IOException {
try {
// TODO: 2016/12/26 设计你自己的响应体解密算法,下面使用了Base64解码
String decodeBody = Base64.decode(responseBody.string());
//Base64解码就上面一行,大家可以自行设计算法
Log.e("AesJsonFactory:", decodeBody);
JsonReader jsonReader = gson.newJsonReader(new StringReader(decodeBody));
try {
return adapter.read(jsonReader);
} finally {
responseBody.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
RequestBodyConverter
请求体操作,如果相对请求体进行操作就在这里写吧,不过我没有这个需求
/**
* 请求体
* Created by ChenSS on 2016/12/26.
*/
final class AesJsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private static final Charset UTF_8 = Charset.forName("UTF-8");
private final Gson gson;
private final TypeAdapter<T> adapter;
AesJsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
this.gson = gson;
this.adapter = adapter;
}
@Override public RequestBody convert(T value) throws IOException {
Buffer buffer = new Buffer();
Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
JsonWriter jsonWriter = gson.newJsonWriter(writer);
// TODO: 2016/12/26 网络请求,这里我没修改,这和Converter-gson是一样的
adapter.write(jsonWriter, value);
jsonWriter.close();
return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
}