/** * Returns a {@link Converter} for {@code type} to {@link RequestBody} from the available * {@linkplain #converterFactories() factories}. * * @throws IllegalArgumentException if no converter available for {@code type}. */ public <T> Converter<T, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations) { return nextRequestBodyConverter(null, type, parameterAnnotations, methodAnnotations); }
/** * Returns a {@link Converter} for {@code type} to {@link RequestBody} from the available * {@linkplain #converterFactories() factories}. * * @throws IllegalArgumentException if no converter available for {@code type}. */ public <T> Converter<T, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations) { return nextRequestBodyConverter(null, type, parameterAnnotations, methodAnnotations); }
@Override public @Nullable Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { boolean isBody = false; boolean isChunked = false; for (Annotation annotation : parameterAnnotations) { isBody |= annotation instanceof Body; isChunked |= annotation instanceof Chunked; } if (!isBody || !isChunked) { return null; } // Look up the real converter to delegate to. final Converter<Object, RequestBody> delegate = retrofit.nextRequestBodyConverter(this, type, parameterAnnotations, methodAnnotations); // Wrap it in a Converter which removes the content length from the delegate's body. return new Converter<Object, RequestBody>() { @Override public RequestBody convert(Object value) throws IOException { final RequestBody realBody = delegate.convert(value); return new RequestBody() { @Override public MediaType contentType() { return realBody.contentType(); } @Override public void writeTo(BufferedSink sink) throws IOException { realBody.writeTo(sink); } }; } }; } }
@Override public RequestBody convert(T value) throws IOException { Class<?> cls = value.getClass(); Converter<T, RequestBody> requestBodyConverter; synchronized (cache) { requestBodyConverter = cache.get(cls); } if (requestBodyConverter == null) { requestBodyConverter = retrofit.nextRequestBodyConverter( skipPast, cls, parameterAnnotations, methodsAnnotations ); synchronized (cache) { cache.put(cls, requestBodyConverter); } } return requestBodyConverter.convert(value); } }
@Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] annotations, Annotation[] methodAnnotations, Retrofit retrofit) { JsonRPC methodAnnotation = Utils.findAnnotation(methodAnnotations, JsonRPC.class); if (methodAnnotation == null) { return null; } String method = methodAnnotation.value(); Converter<JsonRPCRequest, RequestBody> delegate = retrofit.nextRequestBodyConverter(this, JsonRPCRequest.class, annotations, methodAnnotations); //noinspection unchecked return new JsonRPCRequestBodyConverter(method, delegate); }