改造 JSON 对象 Post
Retrofit JSON OBJECT Post
我想向 post 请求发送一个 json 对象。
Json 有下一个结构:
{ "email:"test@test.com",
"password": "test",
"hash": "true"
}
这在 POSTMAN 中很好用,但我不知道如何像在 POSTMAN 中那样在改造中定义密钥。
此刻,我创建了这样一个@Body:
public class LoginRequest {
private String email;
private String password;
private String gethash;
public LoginRequest(String email, String password, String hash) {
this.email = email;
this.password = password;
this.gethash = hash;
}
}
但我真的不知道我必须在哪里定义密钥。
然后,我尝试这样调用 POST 请求:
在端点定义中使用 @Field("json")
而不是 @Body
:
@POST("/login")
public Observable<DataLogin> getLogin(@Field("json") LoginRequest loginRequest);
此外,您必须使用转换器来转换对象。这是 GSON 的示例。您基本上需要为默认 GsonConverterFactory
创建一个自定义包装器,因为它没有实现 stringConverter(...)
方法,该方法将用 @Field
等注释的值转换为字符串。
public class GsonStringConverterFactoryWrapper extends Converter.Factory {
private GsonConverterFactory converterFactory;
public static GsonStringConverterFactoryWrapper create() {
return create(new Gson());
}
@SuppressWarnings("ConstantConditions")
public static GsonStringConverterFactoryWrapper create(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
return new GsonStringConverterFactoryWrapper(gson);
}
private final Gson gson;
private GsonStringConverterFactoryWrapper(Gson gson) {
this.gson = gson;
converterFactory = GsonConverterFactory.create(gson);
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return converterFactory.responseBodyConverter(type, annotations, retrofit);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return converterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
}
@Nullable
@Override
public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new GsonStringConverter<>(gson);
}
public static class GsonStringConverter<T> implements Converter<T, String> {
private final Gson gson;
GsonStringConverter(Gson gson) {
this.gson = gson;
}
@Override
public String convert(@NonNull T value) throws IOException {
return gson.toJson(value);
}
}
}
然后,当您创建 Retrofit 实例时,只需使用此适配器:
new Retrofit.Builder()
.addConverterFactory(GsonStringConverterFactoryWrapper.create(gson)) // if you don't have a custom GSON instance, just call .create()
// [...] other settings
.build();
@FormUrlEncoded
@POST("/login")
public Observable<DataLogin> getLogin(@Field("json") String json);
我想向 post 请求发送一个 json 对象。
Json 有下一个结构:
{ "email:"test@test.com",
"password": "test",
"hash": "true"
}
这在 POSTMAN 中很好用,但我不知道如何像在 POSTMAN 中那样在改造中定义密钥。
此刻,我创建了这样一个@Body:
public class LoginRequest {
private String email;
private String password;
private String gethash;
public LoginRequest(String email, String password, String hash) {
this.email = email;
this.password = password;
this.gethash = hash;
}
}
但我真的不知道我必须在哪里定义密钥。
然后,我尝试这样调用 POST 请求:
在端点定义中使用 @Field("json")
而不是 @Body
:
@POST("/login")
public Observable<DataLogin> getLogin(@Field("json") LoginRequest loginRequest);
此外,您必须使用转换器来转换对象。这是 GSON 的示例。您基本上需要为默认 GsonConverterFactory
创建一个自定义包装器,因为它没有实现 stringConverter(...)
方法,该方法将用 @Field
等注释的值转换为字符串。
public class GsonStringConverterFactoryWrapper extends Converter.Factory {
private GsonConverterFactory converterFactory;
public static GsonStringConverterFactoryWrapper create() {
return create(new Gson());
}
@SuppressWarnings("ConstantConditions")
public static GsonStringConverterFactoryWrapper create(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
return new GsonStringConverterFactoryWrapper(gson);
}
private final Gson gson;
private GsonStringConverterFactoryWrapper(Gson gson) {
this.gson = gson;
converterFactory = GsonConverterFactory.create(gson);
}
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
return converterFactory.responseBodyConverter(type, annotations, retrofit);
}
@Override
public Converter<?, RequestBody> requestBodyConverter(Type type,
Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
return converterFactory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
}
@Nullable
@Override
public Converter<?, String> stringConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new GsonStringConverter<>(gson);
}
public static class GsonStringConverter<T> implements Converter<T, String> {
private final Gson gson;
GsonStringConverter(Gson gson) {
this.gson = gson;
}
@Override
public String convert(@NonNull T value) throws IOException {
return gson.toJson(value);
}
}
}
然后,当您创建 Retrofit 实例时,只需使用此适配器:
new Retrofit.Builder()
.addConverterFactory(GsonStringConverterFactoryWrapper.create(gson)) // if you don't have a custom GSON instance, just call .create()
// [...] other settings
.build();
@FormUrlEncoded
@POST("/login")
public Observable<DataLogin> getLogin(@Field("json") String json);