Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用改型添加JSON请求?_Java_Json_Api_Retrofit_Payload - Fatal编程技术网

Java 如何使用改型添加JSON请求?

Java 如何使用改型添加JSON请求?,java,json,api,retrofit,payload,Java,Json,Api,Retrofit,Payload,我正试图通过改造添加一个JSON请求,因为在我能够提取数据之前,服务器需要一个JSON请求 这是我要添加的请求: {"VALID_FROM":"2018-01-01","VALID_TO":"2019-03-15"} 如果邮递员中没有有效载荷,我将收到此消息 不正确的负载数据:处理消息负载时出错 缺少元素“VALID_FROM” 这是我的API接口 Java public interface SapRestApi { @Headers("Accept: applicati

我正试图通过改造添加一个JSON请求,因为在我能够提取数据之前,服务器需要一个JSON请求

这是我要添加的请求:

{"VALID_FROM":"2018-01-01","VALID_TO":"2019-03-15"}
如果邮递员中没有有效载荷,我将收到此消息

不正确的负载数据:处理消息负载时出错 缺少元素“VALID_FROM”

这是我的API接口 Java

public interface SapRestApi {       
    @Headers("Accept: application/json")
    @GET("/get_users")
    Call<UserList> getUserList();
}
这就是我调用getUserList()的地方

public UserList getUserList(){
UserList userAccounts=null;
OkHttpClient.Builder httpClient=新建OkHttpClient.Builder();
改装改装=新改装.Builder()
.baseUrl(服务器)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient
.addInterceptor(新的基本Interceptor(用户名、密码))
.build())
.build();
SapRestApi userAPI=reformation.create(SapRestApi.class);
调用callSync=userAPI.getUserList();
试一试{
Response=callSync.execute();
System.out.println(“在调用同步执行方法之后创建响应”);
System.out.println(response.toString());
userAccounts=response.body();
}捕获(IOE异常){
e、 printStackTrace();
}
返回用户帐户;
}
每次没有有效负载时,我都会得到一个错误500。 谢谢

public class User implements POJO {

    @SerializedName("USERNAME")
    private String USERNAME;
    @SerializedName("VALID_FROM")
    private String VALID_FROM;

    @SerializedName("VALID_TO")
    private String VALID_TO;

    public String getUserName() {
        return USERNAME;
    }

    public String getValidFrom() {
        return VALID_FROM;
    }

    public String getValidTo() {
        return VALID_TO;
    }

    public void setUserName(String USERNAME) {
        this.USERNAME = USERNAME;
    }
    public void setValidFrom(String VALID_FROM) {
        this.VALID_FROM = VALID_FROM;
    }

    public void setValidTo(String VALID_TO) {
        this.VALID_TO = VALID_TO;
    }
}
private String USERNAME;
public UserList getUserList() {     
    UserList userAccounts = null;

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    Retrofit retrofit = new Retrofit.Builder()
              .baseUrl(server)
              .addConverterFactory(GsonConverterFactory.create())

              .client(httpClient
                      .addInterceptor(new BasicAuthInterceptor(userName, password))
                      .build())

              .build();

    SapRestApi userAPI = retrofit.create(SapRestApi.class);

    Call<UserList> callSync = userAPI.getUserList();



    try {




        Response<UserList> response = callSync.execute();

        System.out.println("Creating Response after Call Sync Execute Method ");
        System.out.println(response.toString());

        userAccounts = response.body();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return userAccounts;
}