使用 Retrofit 从 API Post 调用获取错误请求
Getting Bad Request from API Post Call using Retrofit
我在 PartnerController class 中创建了下一个 savePartner() 方法,如下所示:
public void savePartner(View partnerForm) {
context = partnerForm.getContext();
PartnerDto partner = createPartner(partnerForm);
String jsonPartner = convert(partner);
Call<String> call = appAPI.savePartner("application/json", jsonPartner);
Log.i(TAG, "getPartners submitted to API.");
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()) {
String responseCall = response.body();
} else {
System.out.println(response.errorBody());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
TableRow rowHeader = new TableRow(context);
TextView name = new TextView(context);
name.setText(t.getMessage());
rowHeader.addView(name);
//partnerForm.addView(rowHeader);
t.printStackTrace();
}
});
}
并且我已经将方法 savePartner 添加到改造接口中:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface IApplicationApi {
@GET("Partner/")
//Call<List<PartnerDto>> loadPartners(@Header("Authorization") String authorization);
Call<List<PartnerDto>> loadPartners();
@POST("Partner/")
Call<String> savePartner(@Header("Content-Type") String content_type, @Body String partner);
}
当我在 postman works 中执行 post 调用时(代码 200),但我在 android-studio 中调试了前一个,我得到了下一个错误:
Response{protocol=http/1.1, code=400, message=Bad Request, url=https://localhost/Partner/}
而且我无法获得有关该错误的更多信息。请求是下一个:
Request{method=POST, url=https://localhost/Partner/, tags={class retrofit2.Invocation=administracion.MyProject.APIService.IApplicationApi.savePartner() [application/json, {"email":null,"id":4,"lastname":null,"name":"me","phonenumber":0,"productamount":0.0,"productquantity":0.0,"registereddate":"2021-02-10T00:00:00"}]}}
我把这些值放在 postman 身上,它就像一个魅力。我不知道为什么这个请求不好。有人可以给我一些线索吗?
在此先感谢您的帮助! ^^
2021 年 1 月 3 日更新
我可以使用 httplogginginterceptor 找出错误的原因,我分享这个以防有人需要它:)
尝试替换这个@POST("Partner/")
与@POST("合作伙伴")
您可以使用 HttpLoggingInterceptor 并记录您的请求。希望您在 json 正文或请求正文
中遗漏了一个字段
我在 PartnerController class 中创建了下一个 savePartner() 方法,如下所示:
public void savePartner(View partnerForm) {
context = partnerForm.getContext();
PartnerDto partner = createPartner(partnerForm);
String jsonPartner = convert(partner);
Call<String> call = appAPI.savePartner("application/json", jsonPartner);
Log.i(TAG, "getPartners submitted to API.");
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if(response.isSuccessful()) {
String responseCall = response.body();
} else {
System.out.println(response.errorBody());
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
TableRow rowHeader = new TableRow(context);
TextView name = new TextView(context);
name.setText(t.getMessage());
rowHeader.addView(name);
//partnerForm.addView(rowHeader);
t.printStackTrace();
}
});
}
并且我已经将方法 savePartner 添加到改造接口中:
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface IApplicationApi {
@GET("Partner/")
//Call<List<PartnerDto>> loadPartners(@Header("Authorization") String authorization);
Call<List<PartnerDto>> loadPartners();
@POST("Partner/")
Call<String> savePartner(@Header("Content-Type") String content_type, @Body String partner);
}
当我在 postman works 中执行 post 调用时(代码 200),但我在 android-studio 中调试了前一个,我得到了下一个错误:
Response{protocol=http/1.1, code=400, message=Bad Request, url=https://localhost/Partner/}
而且我无法获得有关该错误的更多信息。请求是下一个:
Request{method=POST, url=https://localhost/Partner/, tags={class retrofit2.Invocation=administracion.MyProject.APIService.IApplicationApi.savePartner() [application/json, {"email":null,"id":4,"lastname":null,"name":"me","phonenumber":0,"productamount":0.0,"productquantity":0.0,"registereddate":"2021-02-10T00:00:00"}]}}
我把这些值放在 postman 身上,它就像一个魅力。我不知道为什么这个请求不好。有人可以给我一些线索吗?
在此先感谢您的帮助! ^^
2021 年 1 月 3 日更新
我可以使用 httplogginginterceptor 找出错误的原因,我分享这个以防有人需要它:)
尝试替换这个@POST("Partner/") 与@POST("合作伙伴")
您可以使用 HttpLoggingInterceptor 并记录您的请求。希望您在 json 正文或请求正文
中遗漏了一个字段