如何使用Retrofit将JSON正文添加到我的帖子请求中?

时间:2019-11-11 13:31:47

标签: java android post retrofit

我正在尝试使用Retrofit库发送POST请求。

这是我的MarketApiCalls界面和我的联网方法正在完成-

public interface MarketApiCalls {


    @POST("api/Search/Vendor/Multiple")
    Call<String> getVendors(
            @Query("take") int take,
            @Query("page") int page,
            @Body String json
    );
}
private void initNetworking() {
        String body = "[{ \"filters\": { \"VendorName\": { \"value\": [\"*\"], \"cretiria\": 0, \"type\": 5 } } }]"
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://search.myverte.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        marketApiCalls = retrofit.create(MarketApiCalls.class);

        Call<String> vendorsCall = marketApiCalls.getVendors(9, 0, body);
        vendorsCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(getContext(), "Not successful", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.d("response body", response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

这是我需要作为正文附加到POST请求的JSON-

[{
    "filters": {
        "VendorName": { 
            "value": ["*"],
            "cretiria": 0,
            "type": 5
        }
    }
}]

问题是我收到错误代码400。它没有指定主体丢失或损坏,仅给出400错误,提示以下错误-

enter image description here

我想念什么?我怀疑自己没有按需给予身体。

编辑-

我尝试了以下解决方案,但发生了相同的错误-

public class VendorBodyModel {
    private Filters filters;

    public VendorBodyModel() {
    }

    public VendorBodyModel(Filters filters) {
        this.filters = filters;
    }

    public Filters getFilters() {
        return filters;
    }

    public void setFilters(Filters filters) {
        this.filters = filters;
    }
    public class Filters {
        private VendorName vendorName;

        public Filters() {
        }

        public Filters(VendorName vendorName) {
            this.vendorName = vendorName;
        }
    }

    public class VendorName {
        private String[] value;
        private int cretiria;
        private int type;

        public VendorName(String[] value, int cretiria, int type) {
            this.value = value;
            this.cretiria = cretiria;
            this.type = type;
        }

        public String[] getValue() {
            return value;
        }

        public void setValue(String[] value) {
            this.value = value;
        }

        public int getCretiria() {
            return cretiria;
        }

        public void setCretiria(int cretiria) {
            this.cretiria = cretiria;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }
    }
}
public interface MarketApiCalls {


    @POST("api/Search/Vendor/Multiple")
    Call<String> getVendors(
            @Query("take") int take,
            @Query("page") int page,
            @Body VendorBodyModel bodyModel
    );
}
private void initNetworking() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://search.myverte.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        marketApiCalls = retrofit.create(MarketApiCalls.class);
        String[] array = {"*"};
        int cretiria = 5;
        int type = 0;
        VendorBodyModel.VendorName vendorName = new VendorBodyModel().new VendorName(array, 5, 0);
        VendorBodyModel.Filters filters = new VendorBodyModel().new Filters(vendorName);
        VendorBodyModel vendorBodyModel = new VendorBodyModel(filters);

        Call<String> vendorsCall = marketApiCalls.getVendors(9, 0, vendorBodyModel);
        vendorsCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (!response.isSuccessful()) {
                    Toast.makeText(getContext(), "Not successful", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(getContext(), "FUCKING SUCCESS!!!", Toast.LENGTH_SHORT).show();
                Log.d("response body", response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

我想念什么?

2 个答案:

答案 0 :(得分:1)

只需像这样创建后模型类:


class Body {
    Filters filters;
    int parameter2;
}

class Filters {
    VendorName VendorName;
    .......
    .......
    .......
}

并像这样使用它:

 @POST("api/Search/Vendor/Multiple")
    Call<String> getVendors(
            @Query("take") int take,
            @Query("page") int page,
            @Body Body json
    );

还要确保Body类的字段命名与所需的命名匹配,也不要忘记列表或数组标记有[...],简单的pojo {...}

这里是科特林的全身课程:

data class Body(
    @SerializedName("filters")
    val filters: Filters? = Filters()
)

data class Filters(
    @SerializedName("VendorName")
    val vendorName: VendorName? = VendorName()
)

data class VendorName(
    @SerializedName("value")
    val value: List<String?>? = listOf(),
    @SerializedName("cretiria")
    val cretiria: Int? = 0,
    @SerializedName("type")
    val type: Int? = 0
)

答案 1 :(得分:0)

创建JsonObject而不是字符串,并将其作为@Body JsonObject request

发送