2

I am expecting both json and xml response in my app. Retrofit 2.0 allows you to add multiple Converter Factories for such situations.

But it seems the order is of utmost importance here. Adding JacksonConverterFactory above SimpleXmlConverterFactory makes Retrofit accept only Json response and throws exception when it encounters XML and vice versa.

Below is a code snippet of how to add multiple addConverterFactory to your Retrofit Builder.

.addConverterFactory(JacksonConverterFactory.create(objectMapper))
                .addConverterFactory(SimpleXmlConverterFactory.create())

<<< edit

Changed the above code to this, but still not working:

return new Retrofit.Builder()
                .client(clientBuilder.build())
                .baseUrl(BuildConfig.API_ENDPOINT)
                .addCallAdapterFactory(unAuthorizedHandlingCallAdapterFactory)
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .addConverterFactory(new QualifiedTypeConverterFactory(JacksonConverterFactory.create(objectMapper), SimpleXmlConverterFactory.create()))
                .build();

edit2

Adding the response type was the key @GET("/") @Xml

Shishir Shetty
  • 2,021
  • 3
  • 20
  • 35

1 Answers1

5

You can combine multiple Converter Factories into a single Converter Factory, check this example from retrofit samples.

Akaki Kapanadze
  • 2,552
  • 2
  • 11
  • 21