Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues while integrating Simple XML converter in retrofit for Android

I have faced couple of issues while integrating Simple XML converter in retrofit for Android.
1). I was getting compilation error after I have added "compile ('com.squareup.retrofit:converter-simplexml:1.9.0')" in build.gradle.

Error Report: Information:Gradle tasks [:app:assembleDebug] Warning:Dependency xpp3:xpp3:1.1.3.3 is ignored for debug as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages Warning:Dependency xpp3:xpp3:1.1.3.3 is ignored for release as it may be conflicting with the internal version provided by Android. In case of problem, please repackage it with jarjar to change the class packages

2). I have created Java Schemas with the help of this http://pojo.sodhanalibrary.com/Convert by providing root class name. And I keep getting InstantiateException or ElementException for one or the other element and mazorly retrofit.converter.ConversionException: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=description, required=true, type=void) on field "description". Same error for many fields.

like image 702
cgr Avatar asked Aug 09 '15 16:08

cgr


1 Answers

ISSUE 1).
I had to add following to my build.gradle. As these packages comes with Android already so there were issues.

compile ('com.squareup.retrofit:converter-simplexml:1.9.0') {
        exclude group: 'xpp3', module: 'xpp3'
        exclude group: 'stax', module: 'stax-api'
        exclude group: 'stax', module: 'stax'
    }

ISSUE 2).
I had to have the Java class hierarchy that should start with the first element that is in XML. I had "Rss" as first element in my XML. And while I generated the schemas using http://pojo.sodhanalibrary.com/Convert, I gave my own name something like "Warnings" that generated classes for all required elements. And Warnings.java had "Rss" instance member. Instead of passing Rss, I had passed Warnings to retrofit.Callback.

Class Warnings {
   private Rss rss;
   .....
   .....
}  

Class Rss {
   private Channel channel;
   .....
   .....
}  

It could find hierarchy of classes to some extent but it started giving errors/exceptions mentioned in question for every fields of all classes though they seem to be available.
After quite a big time spent on resolving these errors, I have changed the response class that was passed to retrofit.Callback from Warnings to Rss and that worked.

XML:

<rss version="2.0">
<channel>...</channel>
</rss>

This was my retrofit request code with errors:

retrofitService.getWarnings(lat, lon, authKey, new Callback<Warnings>() {
            @Override
            public void success(WarningsResponse warningsResponse, Response response) {
            // handle success response..
            }

            @Override
            public void failure(RetrofitError error) {
            // handle error response
              }
        });

Working code:

retrofitService.getWarnings(lat, lon, authKey, new Callback<Rss>() {
            @Override
            public void success(WarningsResponse warningsResponse, Response response) {
            // handle success response..
            }

            @Override
            public void failure(RetrofitError error) {
            // handle error response
              }
        });

The only diff is the class passed to callback.

So, use the class name of root element in XML to pass to Callback so the XML to Java object conversion would easily be successful.

You can find this discussion in XML integration to Retrofit help site:
https://futurestud.io/blog/retrofit-how-to-integrate-xml-converter/

like image 136
cgr Avatar answered Sep 30 '22 02:09

cgr