Skip to content

Instantly share code, notes, and snippets.

@oldergod
Last active July 6, 2023 23:48
Show Gist options
  • Save oldergod/72e96816473c950f9910db35764194d7 to your computer and use it in GitHub Desktop.
Save oldergod/72e96816473c950f9910db35764194d7 to your computer and use it in GitHub Desktop.

Revisions

  1. oldergod revised this gist Dec 6, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions XmlOrJsonConverterFactory.java
    Original file line number Diff line number Diff line change
    @@ -20,16 +20,16 @@ public class XmlOrJsonConverterFactory extends Converter.Factory {
    }
    }

    // and we would simple build retrofit with
    new Retrofit.Builder()
    .addConverterFactory(new XmlOrJsonConverterFactory())
    .build();

    // and my service would be something like
    @Json
    @POST("/some/rest/api")
    Call<Stuff> getStuff();

    @Xml
    @POST("/some/soap/api")
    Call<OtherStuff getOtherStuff();
    Call<OtherStuff getOtherStuff();

    // and we would simple build retrofit with
    new Retrofit.Builder()
    .addConverterFactory(new XmlOrJsonConverterFactory())
    .build();
  2. oldergod revised this gist Dec 6, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions XmlOrJsonConverterFactory.java
    Original file line number Diff line number Diff line change
    @@ -24,3 +24,12 @@ public class XmlOrJsonConverterFactory extends Converter.Factory {
    new Retrofit.Builder()
    .addConverterFactory(new XmlOrJsonConverterFactory())
    .build();

    // and my service would be something like
    @Json
    @POST("/some/rest/api")
    Call<Stuff> getStuff();

    @Xml
    @POST("/some/soap/api")
    Call<OtherStuff getOtherStuff();
  3. oldergod created this gist Dec 6, 2016.
    26 changes: 26 additions & 0 deletions XmlOrJsonConverterFactory.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    public class XmlOrJsonConverterFactory extends Converter.Factory {
    final Converter.Factory xml = SimpleXmlConverterFactory.create();
    final Converter.Factory gson = GsonConverterFactory.create();

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(
    Type type, Annotation[] annotations, Retrofit retrofit) {

    // Retrofit gives us all the annotations so we just need to check
    for (Annotation annotation : annotations) {
    if (annotation.annotationType() == Xml.class) {
    return xml.responseBodyConverter(type, annotations, retrofit);
    }
    if (annotation.annotationType() == Json.class) {
    return gson.responseBodyConverter(type, annotations, retrofit);
    }
    }
    // There is no annotation so we cannot handle it
    return null;
    }
    }

    // and we would simple build retrofit with
    new Retrofit.Builder()
    .addConverterFactory(new XmlOrJsonConverterFactory())
    .build();