改进JSON OBJECT Post

时间:2017-07-23 18:52:03

标签: android json retrofit rx-java rx-android

我想发送一个json对象来发布请求。

Json有下一个结构:

{ "email:"test@test.com",
  "password": "test",
   "hash": "true"
}

这在POSTMAN中效果不错,但我不知道如何在POSTMAN中定义改装中的密钥。

enter image description here

此时,我创建了一个像这样的@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请求: enter image description here

2 个答案:

答案 0 :(得分:2)

在端点定义中使用@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();

答案 1 :(得分:0)

@FormUrlEncoded
@POST("/login")
public Observable<DataLogin> getLogin(@Field("json") String json);