In Android app I'm working with pipeline Retrofit + SimpleXmlConverter to send XMl post requests get XML responses:
@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);
}
In searchHolderByPhoneCard call I send raw String without any headers(my api applies it). The instances of request and response:
<?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>
To parse response I've created non-strict model (I don't have a needy in majority of fields). The code for model:
@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;
}
}
In result, executed api call method throws inside onFailure callback exception with such message:
Caused by: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=Count, required=true) on field 'count' private int
I've read docs about this error and I understand that is related to invalid model structure. But I can't figure out where I'm getting wrong and need your help to fix situation
I have found the solution. The problem is that I was trying to send String raw request as @Body. All "text/plain" requests should be wraped like that:
String requestText = RequestBodyUtil.searchHolderByPhoneCard(phone);
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), requestText);
For this situation it's very usefull to read https://futurestud.io/tutorials/retrofit-2-how-to-send-plain-text-request-body
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.