fastjson的Convert.Factory实现(用fastjson解析Retrofit网络请求返回的数据)

public class FastJsonConverterFactory extends Converter.Factory {
    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json;charset=UTF-8");
    private static final Charset UTF_8 = Charset.forName("utf-8");

    @Override
    public Converter requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        return new Converter() {
            @Override
            public RequestBody convert(T value) throws IOException {
                return RequestBody.create(MEDIA_TYPE,JSON.toJSONString(value).getBytes(UTF_8));
            }
        };
    }

    @Override
    public Converter responseBodyConverter(final Type type, Annotation[] annotations, Retrofit retrofit) {
        return new Converter() {
            @Override
            public T convert(ResponseBody value) throws IOException {
                return JSON.parseObject(value.string(), type);
            }
        };

    }

}

你可能感兴趣的:(android)