简体   繁体   English

Retrofit 错误请求错误:如何做 Retrofit POST 正确的方式

[英]Retrofit Bad Request Error: How to do Retrofit POST the right way

I am having a problem executing a post request using Retrofit.我在使用 Retrofit 执行发布请求时遇到问题。 I keep getting the Bad Request error log response.message()我不断收到错误请求错误日志response.message()

This is the response from Postman GET request to my API:这是 Postman GET 请求对我的 API 的响应:

{
    "data": [
        {
            "id": 7,
            "attributes": {
                "createdAt": "2022-09-14T17:10:38.806Z",
                "updatedAt": "2022-09-14T17:10:38.806Z",
                "publishedAt": "2022-09-14T17:10:38.767Z",
                "title": "This is title",
                "body": "This is body"
            }
        }
    ],
    "meta": {
        "pagination": {
            "page": 1,
            "pageSize": 25,
            "pageCount": 1,
            "total": 1
        }
    }
}

I am able to perform the Retrofit GET and DELETE.我能够执行 Retrofit GET 和 DELETE。 The problem comes when am trying to execute a POST.当我尝试执行 POST 时,问题就来了。 This is the request body that is send using Postman and it is working correctly:这是使用 Postman 发送的请求正文,它工作正常:

{
    "data": {
        "title": "This is title",
        "body": "This is body"
    }
}

How can I replicate this request body to make POST request using Retrofit using Android Java?如何使用 Retrofit 使用 Android Java 复制此请求正文以发出 POST 请求? Here is the code am using which is retuning Bad request error:这是正在重新调整错误请求错误的代码:

My interface:我的界面:

public interface Service {

    @FormUrlEncoded
    @POST("api/notes")
    Call<DataResponse> create(
            @Field("title") String title,
            @Field("body") String body
    );

The method I am excuting to Post data:我正在执行发布数据的方法:

    private void create(){
        //Call the interface
        crudInterface = RestClient.connection().create(Service.class);
        Call<DataResponse> call = crudInterface.create(title, body);
        call.enqueue(new Callback<DataResponse>() {
            @Override
            public void onResponse(@NonNull Call<DataResponse> call, @NonNull Response<DataResponse> response) {
                Toast toast = Toast.makeText(getApplicationContext(), response.message(), Toast.LENGTH_LONG);
                toast.show();
                Log.e("Response : ", response.message());
                Log.d("Response code: ", String.valueOf(response.code()));

            }

            @Override
            public void onFailure(@NonNull Call<DataResponse> call, @NonNull Throwable t) {
                Toast.makeText(getApplicationContext(), "Gagal menghubungi server : "+t.getMessage(), Toast.LENGTH_LONG).show();
                Log.e("Throw err: ",t.getMessage());
            }
        });
    }

I get this to my logcat:我得到这个到我的logcat:

2022-09-14 20:20:33.153 9649-9649/com.application.strapi_crud E/Response :: Bad Request
2022-09-14 20:20:33.252 9649-9649/com.application.strapi_crud D/Response code :: 400

There is a problem with your service interface.您的服务接口有问题。 You are posting this json object:您正在发布此 json object:

{"title":"yourTitleValue", "body":"yourBodyValue"}

However you should send an object like this:但是,您应该像这样发送 object:

{
"data": {
    "title": "This is title",
    "body": "This is body"
    }
}

so, there are a few things you can do but I think the most straightforward soultion is to create a data class:所以,你可以做一些事情,但我认为最直接的方法是创建一个数据 class:

public class Data{
@SerialisedName("title")
private String title;
@SerialisedName("body")
private String body;
//constructors, getters
}

After that, you should change your interface:之后,您应该更改界面:

public interface Service {

@FormUrlEncoded
@POST("api/notes")
Call<DataResponse> create(@Body Data data);
}

Finally, you should create a Data object and pass it to your function like this:最后,您应该创建一个数据 object 并将其传递给您的 function,如下所示:

Call<DataResponse> call = crudInterface.create(new Data("some title", " some 
body"));

If there is not a problem with your DataResponse class, this approch should work.如果您的 DataResponse class 没有问题,这个方法应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM