Retrofit自动定义ConverterFactory

先来一段我们经常写的代码
    private void createRetrofit(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(getHttpClient(getAppIntercepter(),getCache(App.getAppContext())))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

怎么样是不是很简单啊!那大家有没有想过为何addConverterFactory??我当时的理解是addConverterFactory让其支持Gson解析,能让返回的数据直接转换成实体对象

现在的理解:

在请求阶段,我可以将@Body的请求操作做操作,对返回的数据做一定操作例如转换成Gson

返回数据转换器

例如我需要定义一个自己的数据类型转换器?比如数据返回String类型的,可以仿造GsonConverterFactory的写法,进行编写

1:首先定义StringResponseBodyConverter

final class StringResponseBodyConverter implements Converter<ResponseBody, String> {
    public static final StringResponseBodyConverter INSTANCE =new StringResponseBodyConverter();
    @Override
    public String convert(ResponseBody value) throws IOException {
        Log.d("wyz", "转换开始:" + value);
        String s = value.string();
        return s;
    }
}

ResponseBody就是请求网络数据反馈的数据,他是一个流哦,convert方法就是返回的需要对数据处理,进行返回的数据

2:定义StringConverFactory

public class StringConverFactory extends Converter.Factory {
    public static StringConverFactory create() {
        return new StringConverFactory();
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        Log.d("wyz", "读取数据:responseBodyConverter" + type);
        if (type == String.class) {
            Log.d("wyz", "执行开始");
            return StringResponseBodyConverter.INSTANCE;
        }
        return null;
    }

    @Nullable
    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        Log.d("wyz", "请求数据类型:" + type);
        if (type == NetRequest.class){

        }
        return null;
    }
}

我们是要对数据做处理,我们只需要关注responseBodyConverter,首先判断数据类型,返回不同的Converter

请求数据转换器
注意:只针对@Body注册的post请求参数

1:重写requestBodyConverter方法,这次我们要针对这个方法,返回Converter,来看看Gson是怎么写的

final class GsonRequestBodyConverter<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;

  GsonRequestBodyConverter(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.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
  }
}

看convert方法value就是传递的Body对象,首先将对象转换成json字符串,然后调用了RequestBody.create()方法第一个参数,是关键点

private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");

其实这是一个请求的头,告诉服务器我的数据格式是json格式,

举个例子,我们在post请求中使用 @FormUrlEncoded注解,采用的的post表单形式提交数据,其实是在多加了一个请求头

private static final MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");

数据个格式是:ket=value&key1=value

这也就是为何这种形式的post请求可以传递一个@FieldMap的map参数了,key于value对应

如果要提取String类型的post请求没有任何格式,比如说数据经历了某种算法,那你就要定义这样的一个自定义ConverterFactory对请求数据进行加密

并且添加头

public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8");

实战
项目需求所有的请求的@Body数据,最终以Base64编码?如何实现呢??

1:使用OkHttp中的拦截器(后面再讲)

2:自定义ConverterFactory针对请求数据做处理

设计思路,拿来主义,在GsonConverterFactory上进行修改

抄袭大发:

1:MyGsonConverterFactory,不需要改动

public final class MyGsonConverterFactory extends Converter.Factory {

  public static MyGsonConverterFactory create() {
    return create(new Gson());
  }

  public static MyGsonConverterFactory create(Gson gson) {
    return new MyGsonConverterFactory(gson);
  }

  private final Gson gson;

  private MyGsonConverterFactory(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 MyGsonResponseBodyConverter<>(gson, adapter);
  }

  @Override
  public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                        Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new MyGsonRequestBodyConverter<>(gson, adapter);
  }
}

2:MyGsonResponseBodyConverter对返回数据处理成对象,不需要更改

public final class MyGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

  private final Gson gson;
  private final TypeAdapter<T> adapter;


  MyGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
    this.gson = gson;
    this.adapter = adapter;
  }


  @Override public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
      return adapter.read(jsonReader);
    } finally {
      value.close();
    }
  }
}

3:核心到了,需要做的改动到了

public final class MyGsonRequestBodyConverter<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;

  MyGsonRequestBodyConverter(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.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
  }
}

关注:convert方法,我们主需要对最后返回的数据做Base64编码即可,并且修改返回头

public final class MyGsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
    private static final MediaType MEDIA_TYPE = MediaType.parse("text/x-markdown; charset=utf-8");//修改请求头告知请求内容text内容
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    private final Gson gson;
    private final TypeAdapter<T> adapter;

    MyGsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public RequestBody convert(T value) throws IOException {
        String requestData = gson.toJson(value);//得到请求数据
        Log.d("wyz", "请求时数据:" + requestData);
        String base64Data = Base64.encodeToString(requestData.getBytes(), Base64.NO_WRAP);//编码请求数据
        Log.d("wyz", "请求转换后编码数据:" + base64Data);
        return RequestBody.create(MEDIA_TYPE, base64Data);//重新组装请求数据
    }
}
注意只有在@Body的时候,才会调用这个方法
    @POST("/")
    Observable<UserInfo> login(@Body LoginInfo parameters);

我会使用自己的请求方式,这样就可以在项目中把所有的请求数据进行加密,并非通过拦截器做到,拦截器属于OkHtttp中使用,这属于在retrofit层做处理,当然拦截器层也可以做处理





猜你喜欢

转载自blog.csdn.net/wenyingzhi/article/details/80475660