在Android应用中,我正在使用管道翻新+ SimpleXmlConverter发送XMl发布请求以获取XML响应:
@Provides
@AuthorizationScope
Retrofit provideAuthorizationRetrofit(@Named("BASE_CRM_URL") String baseUrl,
OkHttpClient client) {
return new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(SimpleXmlConverterFactory.createNonStrict())
.client(client)
.build();
}
@Provides
@AuthorizationScope
ApiAuthorizationService provideAuthorizationService(Retrofit retrofit) {
return retrofit.create(ApiAuthorizationService.class);
}
public interface ApiAuthorizationService {
@POST("/")
Call<CardHolder> searchHolderByPhoneCard(@Body String messageSearchHolders2);
}
在searchHolderByPhoneCard呼叫中,我发送了没有任何标题的原始String(我的api应用了它)。请求和响应的实例:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message Action="Search holders 2" Terminal_Type="77" Global_Type="ABC" Unit_ID="1" User_ID="1">
<Include>Holder_Card</Include>
<Item Mode="Clear" />
<Item Mode="Add">
<Contacts>
<Phone Value="+79600040318" IsNumber="True" />
</Contacts>
</Item>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Holders Message_ID="0" IndexFrom="1" IndexTo="1" Count="1" Search_GUID="{5BDE0E0A-2CD4-4B96-AD07-2D4BB5A15409}">
<Holder>
<Deleted>No</Deleted>
<Group_ID>1</Group_ID>
<Group_Name>
<![CDATA[Владельцы карт "Перец"]]>
</Group_Name>
<Division_ID>1</Division_ID>
<Division_Name>Головное подразделение</Division_Name>
<Holder_ID>10000000040834</Holder_ID>
<INN></INN>
<External_Code></External_Code>
<Unpay_Type_ID></Unpay_Type_ID>
<Unpay_Type_Name></Unpay_Type_Name>
<L_Name>Ермолин</L_Name>
<F_Name>Павел</F_Name>
<M_Name>Сергеевич</M_Name>
<Full_Name>Ермолин Павел Сергеевич</Full_Name>
<Birth>1992-12-14</Birth>
<Gender>Male</Gender>
<Marrital>Unknown</Marrital>
<Smoke>No</Smoke>
<Verification>Yes</Verification>
<Image>No</Image>
<Language_ID>1049</Language_ID>
<Language_Name>
<![CDATA[[RUS] Русский (Россия)]]>
</Language_Name>
<Source></Source>
<Remarks></Remarks>
<Holders_Cards>
<Holder_Card>
<Holder_ID>10000000040834</Holder_ID>
<Card>
<Card_Code>900221</Card_Code>
<Is_Virtual_Card>Yes</Is_Virtual_Card>
<Is_Confirm_Manager>No</Is_Confirm_Manager>
<Status>Active</Status>
<Carrier_Data></Carrier_Data>
<Offered>2018-08-25</Offered>
<Expired>2019-08-25</Expired>
<Group_ID>5</Group_ID>
<Group_Name>Virtual</Group_Name>
<Holder_ID>10000000040834</Holder_ID>
<Owner_ID></Owner_ID>
<Verification>Yes</Verification>
</Card>
</Holder_Card>
</Holders_Cards>
</Holder>
为了解析响应,我创建了非严格模型(大多数领域中我不需要)。模型代码:
@Root(name = "Holders", strict = false)
public class CardHolder {
@Attribute(name = "Count")
private int count;
@Element (name = "L_Name", required = false)
@Path("Holder")
private String secondName;
@Element (name = "F_Name", required = false)
@Path("Holder")
private String firstName;
@Element (name = "M_Name", required = false)
@Path("Holder")
private String thirdName;
@Element (name = "Birth", required = false)
@Path("Holder")
private String birth;
@Element (name = "Gender")
@Path("Holder")
private String gender;
@Element(name = "is_Virtual_Card", required = false)
@Path("Holder/Holders_Cards/Holder_Card/Card")
private String isVirtualCard;
public CardHolder() {
}
public int getCount() {
return count;
}
public void setCount(final int count) {
this.count = count;
}
public String getIsVirtualCard() {
return isVirtualCard;
}
public void setIsVirtualCard(final String isVirtualCard) {
this.isVirtualCard = isVirtualCard;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(final String secondName) {
this.secondName = secondName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getThirdName() {
return thirdName;
}
public void setThirdName(final String thirdName) {
this.thirdName = thirdName;
}
public String getBirth() {
return birth;
}
public void setBirth(final String birth) {
this.birth = birth;
}
public String getGender() {
return gender;
}
public void setGender(final String gender) {
this.gender = gender;
}
}
结果是,已执行的api调用方法在onFailure回调异常内引发了以下消息:
原因:org.simpleframework.xml.core.ValueRequiredException:无法 满足@ org.simpleframework.xml.Attribute(empty =,name = Count, required = true)字段“ count”的私有int
我已经阅读了有关此错误的文档,并且我知道与无效的模型结构有关。但是我无法弄清楚哪里出了问题,需要您的帮助来解决问题
答案 0 :(得分:0)
我找到了解决方案。问题是我试图将String原始请求发送为@Body。所有“文本/纯文本”请求都应这样包装:
String requestText = RequestBodyUtil.searchHolderByPhoneCard(phone);
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), requestText);
在这种情况下,阅读https://futurestud.io/tutorials/retrofit-2-how-to-send-plain-text-request-body
非常有用