gpt4 book ai didi

android - 从没有模型的单个字符串值格式化 JSON Body for Retrofit

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:04 26 4
gpt4 key购买 nike

有没有办法将单个字符串值(纯文本,而不是 json) 转换为带有注释的 JSON 主体?我不想创建这么简单的模型。

示例

@POST("foo/{fooId}/bars")
Observable<Void> postBar(@Path("fooId") String styleId, @Body BarModel bar);

class BarModel {
public String bar;
}

会给我我所期望的:

{
"bar" : "hello world"
}

有没有一种简单的方法可以用注释来完成?像这样:

@POST("foo/{fooId}/bars")
Observable<Void> postBar(@Path("fooId") String styleId, @Body("bar") String bar);

最佳答案

Retrofit 有一个 Converter.Factory 抽象类,您可以使用它来执行自定义 HTTP 表示。如果该方法具有特定的注释,您可以创建一个 Converter 来构造一个 okhttp.RequestBody

最终结果如下:

@POST("/")
Call<Void> postBar(@Body @Root("bar") String foo)

并转换:postBar("Hello World")进入 { "bar": "Hello World"}

让我们开始吧。

第 1 步 - 为根键 (Root.java) 创建注释

/**
* Denotes the root key of a JSON request.
* <p>
* Simple Example:
* <pre><code>
* &#64;POST("/")
* Call&lt;ResponseBody&gt; example(
* &#64;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();
}

第 2 步 - 定义检测注释的 Converter.Factory (JSONConverterFactory.java)。我正在使用 Gson用于 JSON 解析,但您可以使用任何您想要的框架。

/**
* 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));
}
}
}

第 3 步 - 将 JSONConverterFactory 安装到您的 Retrofit 实例中

Gson gson = new GsonBuilder().create(); // Or your customized version
Retrofit.Builder builder = ...;
builder.addConverterFactory(new JSONConverterFactory(gson))

第 4 步 - 利润

@POST("/")
Call<Void> postBar(@Body @Root("bar") String foo)

或者对于你的情况:

@POST("foo/{fooId}/bars")
Observable<Void> postBar(@Body @Root("bar") String barValue, @Path("fooId") String styleId);

关于android - 从没有模型的单个字符串值格式化 JSON Body for Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38223896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com