android


How to annotation order field when using XStream on Android?


I need to generate XML from java objects on Android. XML nodes must be in
definite sequence. Here is what I got(These fields as according to alphabetical order):
<soap:Envelope xmlns:cwmp="urn:dslforum-org:cwmp-1-0"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<cwmp:Inform>
<CurrentTime>2013-06-07 07:11:20.561 UTC</CurrentTime>
<DeviceId>
<OUI>002615</OUI>
<Manufacturer>Conexant</Manufacturer>
<ProductClass>ADSL2+ IAD</ProductClass>
<SerialNumber>00261559a496</SerialNumber>
</DeviceId>
<Event soapenc:arrayType="cwmp:EventStruct[1]">
<EventStruct>
<CommandKey></CommandKey>
<EventCode>0 BOOTSTRAP</EventCode>
</EventStruct>
</Event>
<ParameterList soapenc:arrayType="cwmp:ParameterValueStruct[1]">
<ParameterValueStruct>
<name>InternetGatewayDevice.DeviceInfo.SpecVersion</name>
<value>1.0</value>
</ParameterValueStruct>
</ParameterList>
<MaxEnvelopes>1</MaxEnvelopes>
<RetryCount>0</RetryCount>
</cwmp:Inform>
</soap:Body>
<soap:Header>
<ID soap:mustUnderstand="1">
<string>00001</string>
</ID>
</soap:Header>
</soap:Envelope>
Here is what I want it to be:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<ID soap:mustUnderstand="1">
<string>00001</string>
</ID>
</soap:Header>
<soap:Body>
<cwmp:Inform>
<DeviceId>
<Manufacturer>Conexant</Manufacturer>
<OUI>002615</OUI>
<ProductClass>ADSL2+ IAD</ProductClass>
<SerialNumber>00261559a496</SerialNumber>
</DeviceId>
<Event soapenc:arrayType="cwmp:EventStruct[1]">
<EventStruct>
<EventCode>0 BOOTSTRAP</EventCode>
<CommandKey></CommandKey>
</EventStruct>
</Event>
<MaxEnvelopes>0</MaxEnvelopes>
<CurrentTime>2013-06-07 07:16:54.128 UTC</CurrentTime>
<RetryCount>0</RetryCount>
<ParameterList soapenc:arrayType="cwmp:ParameterValueStruct[1]">
<ParameterValueStruct>
<name>InternetGatewayDevice.DeviceInfo.SpecVersion</name>
<value>1.0</value>
</ParameterValueStruct>
</ParameterList>
</cwmp:Inform>
</soap:Body>
</soap:Envelope>
I've investigated this problem more. And I found that Xstream handles fields in
wrong order on Android. Xstream handles fields well in Java project.
#XStreamAlias("cwmp:Inform")
public class Inform {
#XStreamAlias("DeviceId")
private DeviceIdStruct deviceId;
#XStreamAlias("Event")
private EventList event;
#XStreamAlias("MaxEnvelopes")
private int maxEnvelopes;
#XStreamAlias("CurrentTime")
private Date currentTime;
#XStreamAlias("RetryCount")
private int retryCount;
#XStreamAlias("ParameterList")
private ParameterValueList parameterList;
}
Here is my answer.
I researched the issue and concluded that Android returns fields in alphabetical order of field types.
You can inherit FieldKeySorter to set order.
First you need create annotation that will define fields order:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface XMLSequence {
String[] value();
}
And example using:
#XMLSequence({
"accountIds",
"addresses",
"birthDate",
"contact",
"name",
"status",
})
Then I've inherited FieldKeySorter :
public class SequenceFieldKeySorter implements FieldKeySorter {
#Override
public Map sort(final Class type, final Map keyedByFieldKey) {
Annotation sequence = type.getAnnotation(XMLSequence.class);
if (sequence != null) {
final String[] fieldsOrder = ((XMLSequence) sequence).value();
Map result = new OrderRetainingMap();
Set<Map.Entry<FieldKey, Field>> fields = keyedByFieldKey.entrySet();
for (String fieldName : fieldsOrder) {
if (fieldName != null) {
for (Map.Entry<FieldKey, Field> fieldEntry : fields) {
if
(fieldName.equals(fieldEntry.getKey().getFieldName())) {
result.put(fieldEntry.getKey(),
fieldEntry.getValue());
}
}
}
}
return result;
} else {
return keyedByFieldKey;
}
}
}
And finally go:
XStream x = new XStream(new PureJavaReflectionProvider(
new FieldDictionary(new SequenceFieldKeySorter())));
You can implement this interface FieldKeySorter for the classes you want to control the order.
You can write an annotation by yourself, which does not only extend the target class but also implement FieldKeySorter at runtime by reflection. Finally replace the instance to control the order.

Related Links

Why is RelativeLayout not enforcing layout_alignLeft with layout_centerInParent view?
Capture size of an ImageView (Image Inside) after loading Glide
chane selected days background colour in calendarview
Node.js - Write after end error
Cancel PendingIntent (Broadcast, AlarmManager) not working
Android: Launching Activity fails from onCreate but works from onActivityResult
ImageView.setImageBitmap() is black
Firebase save Traffic use with setPersistenceEnabled
How to get iBeacon mac address?
Cannot detect beacon after restart the app using Android Beacon Library
Make volley http request during app installation
Anko view from class
Retrive an array from another array - android
Get authorisation to publish on Google Cloud PubSub from an Android app
Is using precompiled C++ Visual Studio library on Android possible?
android ssl without domain android HttpsURLConnection

Categories

HOME
url-redirection
time-complexity
obfuscation
knitr
google-apps
permissions
hugo
vert.x
special-characters
p2p
text-mining
dynatrace
intershop
delphi-xe5
sencha-touch
replication
visual-studio-extensions
monit
interpreter
roundup
dapper-extensions
google-cloud-print
gpio
fragment-backstack
mat
entity-relationship-model
fractions
mql
zend-debugger
turfjs
android-sugarorm
papaparse
joomla2.5
nested-lists
html-encode
roblox
python-pptx
swagger-php
latitude-longitude
openalpr
reactivekit
cd
timing
bc
recurrent-neural-network
urlsession
apollostack
knime
ovf
linter
reportservice2010
video-conferencing
avx
sundials
payload
universal-analytics
javafx-css
cvxpy
simplepie
stax
microstation
greatest-n-per-group
qiime
bizagi
boomerang
roadkill-wiki
poppler
extjs-grid
mod-proxy
sql-server-data-tools
rdflib
keyup
omnifaces
ms-access-2000
zxspectrum
showdialog
jbake
cocos3d
wcm
nsmenu
ignite-ui
ss7
cuba
vbe
multitasking
high-resolution
light
ebay-lms
neos-server
fb.ui
twisted.web
alice
joomla3.1
frameworkelementfactory
objectquery
ikimagebrowserview
lr
cloudfiles
opml
sifr
wordprocessingml
todos
linfu-dynamicproxy
firefox-3
ifilter
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App