java—如何在改型中处理错误空体?

svmlkihl  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(321)

我试着用jwt承载身份验证来更新数据,但是当我调试 response 代码,它的错误 400 ,消息是 body = null 这是我的智力课

@Headers({"accept: text/plain","Content-Type: application/json-patch+json"    })
    @POST("services/app/ProjectService/Create")
    Call<ProjectRequest> postProject(@Header("Authorization") String token,
                                     @Body ProjectRequest projectRequest
    );

这是我的模型课

public class ProjectRequest {
    @SerializedName("name")
    @Expose
    private String name;

    @SerializedName("description")
    @Expose
    private String description;

    @SerializedName("ownerId")
    @Expose
    private int ownerId;

    @SerializedName("startPeriod")
    @Expose
    private String startPeriod;

    @SerializedName("endPeriod")
    @Expose
    private String endPeriod;

    @SerializedName("isDraft")
    @Expose
    private boolean isDraft;

    @SerializedName("ownerName")
    @Expose
    private String ownerName;

    @SerializedName("pitId")
    @Expose
    private int pitId;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(int ownerId) {
        this.ownerId = ownerId;
    }

    public String getStartPeriod() {
        return startPeriod;
    }

    public void setStartPeriod(String startPeriod) {
        this.startPeriod = startPeriod;
    }

    public String getEndPeriod() {
        return endPeriod;
    }

    public void setEndPeriod(String endPeriod) {
        this.endPeriod = endPeriod;
    }

    public boolean isDraft() {
        return isDraft;
    }

    public void setDraft(boolean draft) {
        isDraft = draft;
    }

    public String getOwnerName() {
        return ownerName;
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public int getPitId() {
        return pitId;
    }

    public void setPitId(int pitId) {
        this.pitId = pitId;
    }

主要课程是

private void initApi() {
        projectApi = ApiClient.getClient().create(ProjectApi.class);
    }

    private void postProject(final boolean isOnline) {
        if (isOnline) {
            final ProjectRequest projectRequest = new ProjectRequest();
            projectRequest.setName(projectName);
            projectRequest.setDescription(description);
            projectRequest.setOwnerId(1);
            projectRequest.setStartPeriod(dateStart);
            projectRequest.setEndPeriod(dateEnd);
            projectRequest.setDraft(true);
            projectRequest.setOwnerName("yogi kun");
            projectRequest.setPitId(1);

            Call<ProjectRequest> callProject = projectApi.postProject(" Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6Il and so . . . ",
                    projectRequest);
            callProject.enqueue(new Callback<ProjectRequest>() {
                @Override
                public void onResponse(@NotNull Call<ProjectRequest> call, @NotNull Response<ProjectRequest> response) {
                    if (response.isSuccessful()) {
                        try {
                            Toast.makeText(getContext(), "Success", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            String message = "";
                            if (e.getMessage() != null) {
                                message = e.getMessage();
                            }
                            Toast.makeText(getContext(), "Failed " + message, Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(getContext(), "Failed " + response, Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(@NotNull Call<ProjectRequest> call, @NotNull Throwable t) {
                    Toast.makeText(getContext(), "Failure" + t, Toast.LENGTH_SHORT).show();
                }
            });
}

错误是
在此处输入图像描述

在此处输入图像描述

yhived7q

yhived7q1#

与您所期望的相比,json的格式可能有问题。您是否能够使用postman之类的工具并尝试此api调用来查看它返回的格式?不能说太多,但显示的错误消息是在json中的某个地方找到了某个数组

相关问题