本篇博客的目的只是拿到Retrofit请求回来的原始数据,或者是将Retrofit请求回来的数据通过自定义的Converter实现自定义解析(大神请无视)、
我们在进行网络请求,服务器返回的数据肯定都是定义好格式的数据,比如JSON格式,如果我们使用HttpURLConnection请求服务器数据,大部分是利用Google提供的gson.jar进行数据格式转换。
使用Retrofit请求的数据进行格式转换有更简洁的方式,就像官方文档叙述的一样首先要引入以下两个库(为了异步操作,我也引入的RxJava的库)
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
//RxJava的库
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'io.reactivex:rxandroid:1.2.0'
正常的请求写法如下所示
retrofit = new Retrofit.Builder().baseUrl(Global.URLPATH)
.client(new OkHttpClient())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
这种写法只能解析及其标准的JSON数据,但是真实开发中,经常会有调试的情况,或者服务器返回的JSON和自己定义的JSON实体类字段不一致的情况,可是以上写法并不能看到原生的JSON串。调试很困难,而上述代码的JSON转换全是通过GsonConverterFactory类调用实现的,我们可以自定义该类实现自定义效果。具体代码如下:
retrofit = new Retrofit.Builder().baseUrl(Global.URLPATH)
.client(new OkHttpClient())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(DecodeConverterFactory.create())
.build();
public class DecodeConverterFactory extends Converter.Factory {
public static DecodeConverterFactory create() {
return create(new Gson());
}
public static DecodeConverterFactory create(Gson gson) {
return new DecodeConverterFactory(gson);
}
private final Gson gson;
private DecodeConverterFactory(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 DecodeResponseBodyConverter<>(adapter);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
return new DecodeRequestBodyConverter<>(gson, adapter);
}
}
public class DecodeRequestBodyConverter<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;
DecodeRequestBodyConverter(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);
adapter.write(jsonWriter,value);
jsonWriter.flush();
return RequestBody.create(MEDIA_TYPE,buffer.readByteString());
}
}
该类注释位置为请求到的原始数据
public class DecodeResponseBodyConverter<T> implements Converter<ResponseBody, T> {
private final TypeAdapter<T> adapter;
DecodeResponseBodyConverter(TypeAdapter<T> adapter) {
this.adapter = adapter;
}
@Override
public T convert(ResponseBody value) throws IOException {
//解密字符串
String temp = new String(value.bytes(), "utf-8");
// String response = value.string();
return adapter.fromJson(temp);
}
}