大佬教程收集整理的这篇文章主要介绍了使用 Retrofit 从 API Post Call 获取错误请求,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 PartnerController 类中创建了下一个 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);
}
当我在邮递员工作(代码 200)中执行 post 调用时,但我在 androID-studio 中调试了前一个并获得了下一个错误:
响应{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"}]}}
我把这些价值观放在邮递员身上,它就像一个魅力。我不知道为什么这个要求不好。有人能给我一些线索吗?
在此先感谢您的帮助! ^^
2021 年 1 月 3 日更新
我可以使用 httplogginginterceptor 获取错误原因,我将其分享以备更多人需要它:)
https://howtodoinjava.com/retrofit2/logging-with-retrofit2/
您可以使用 httpLoggingInterceptor 并记录您的请求。我希望您在 json 正文或请求正文中缺少一个字段
,尝试替换这个@POST("Partner/") 与@POST("合作伙伴")
以上是大佬教程为你收集整理的使用 Retrofit 从 API Post Call 获取错误请求全部内容,希望文章能够帮你解决使用 Retrofit 从 API Post Call 获取错误请求所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。