我正在尝试使用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错误,提示以下错误-
我想念什么?我怀疑自己没有按需给予身体。
编辑-
我尝试了以下解决方案,但发生了相同的错误-
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) {
}
});
}
我想念什么?