PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/gzsll/hupu/components/retrofit/FastJsonRequestBodyConverter.java

https://gitlab.com/gzsll/TLint
Java | 28 lines | 20 code | 5 blank | 3 comment | 0 complexity | 972fc163ea9e2d3f27d0715ed6f26b8f MD5 | raw file
  1. package com.gzsll.hupu.components.retrofit;
  2. import com.alibaba.fastjson.JSON;
  3. import java.io.IOException;
  4. import java.lang.reflect.Type;
  5. import java.nio.charset.Charset;
  6. import okhttp3.MediaType;
  7. import okhttp3.RequestBody;
  8. import retrofit2.Converter;
  9. /**
  10. * Created by sll on 2016/3/31.
  11. */
  12. public class FastJsonRequestBodyConverter<T> implements Converter<T, RequestBody> {
  13. private Type type;
  14. private Charset charset;
  15. private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
  16. public FastJsonRequestBodyConverter(Type type, Charset charset) {
  17. this.type = type;
  18. this.charset = charset;
  19. }
  20. @Override public RequestBody convert(T value) throws IOException {
  21. return RequestBody.create(MEDIA_TYPE, JSON.toJSONString(value).getBytes(charset));
  22. }
  23. }