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

我正在尝试使用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,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错误,提示以下错误-

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

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

编辑-

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

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,@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,vendorBodyModel);
        vendorsCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call,Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(getcontext(),"FUCKING SUCCESS!!!",Toast.LENGTH_SHORT).show();
                Log.d("response body",Throwable t) {

            }
        });
    }

我想念什么?

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

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


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
)
,

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

发送
本文链接:https://www.f2er.com/3123875.html

大家都在问