Retrofit ha una classe astratta Converter.Factory
che è possibile utilizzare per fare una rappresentazione HTTP personalizzata. È possibile creare un convertitore per costruire un okhttp.RequestBody
se il metodo ha un'annotazione specifica.
Il risultato finale sarà simile:
@POST("/")
Call<Void> postBar(@Body @Root("bar") String foo)
e trasformare: postBar("Hello World")
in { "bar" : "Hello World" }
.
Iniziamo.
Fase 1 - creare un'annotazione per la chiave principale (Root.java)
/**
* Denotes the root key of a JSON request.
* <p>
* Simple Example:
* <pre><code>
* @POST("/")
* Call<ResponseBody> example(
* @Root("name") String yourName);
* </code></pre>
* Calling with {@code foo.example("Bob")} yields a request body of
* <code>{name=>"Bob"}</code>.
* @see JSONConverterFactory
*/
@Documented
@Target(PARAMETER)
@Retention(RUNTIME)
public @interface Root {
/**
* The value of the JSON root.
* Results in {"value" : object}
*/
String value();
}
Fase 2 - definire il Converter.Factory che rileva l'annotazione (JSONConverterFactory.java). Sto usando Gson per l'analisi JSON ma puoi usare qualsiasi framework tu voglia.
/**
* Converts @Root("key") Value to {"key":json value} using the provided Gson converter.
*/
class JSONConverterFactory extends Converter.Factory {
private final Gson gson;
private static final MediaType CONTENT_TYPE =
MediaType.parse("application/json");
JSONConverterFactory(Gson gson) {
this.gson = gson;
}
@Override
public Converter<?, RequestBody> requestBodyConverter(
Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
for (Annotation annotation : parameterAnnotations) {
if (annotation instanceof Root) {
Root rootAnnotation = (Root) annotation;
return new JSONRootConverter<>(gson, rootAnnotation.value());
}
}
return null;
}
private final class JSONRootConverter<T> implements Converter<T, RequestBody> {
private Gson gson;
private String rootKey;
private JSONRootConverter(Gson gson, String rootKey) {
this.gson = gson;
this.rootKey = rootKey;
}
@Override
public RequestBody convert(T value) throws IOException {
JsonElement element = gson.toJsonTree(value);
JsonObject object = new JsonObject();
object.add(this.rootKey, element);
return RequestBody.create(CONTENT_TYPE, this.gson.toJson(object));
}
}
}
Fase 3 - installare il JSONConverterFactory nella tua istanza retrofit
Gson gson = new GsonBuilder().create(); // Or your customized version
Retrofit.Builder builder = ...;
builder.addConverterFactory(new JSONConverterFactory(gson))
Fase 4 - Utile
@POST("/")
Call<Void> postBar(@Body @Root("bar") String foo)
O per il vostro caso:
@POST("foo/{fooId}/bars")
Observable<Void> postBar(@Body @Root("bar") String barValue, @Path("fooId") String styleId);
Vedi anche questo [meta-discussione] (http://meta.stackoverflow.com/q/327487/230513). – trashgod