Multiple converters with Retrofit 2

后端 未结 4 1004
萌比男神i
萌比男神i 2020-12-13 10:30

I have a HATEOAS (HAL) REST service and managed to talk to it with the code below (using halarious as a conversion engine) but when I try to merge the converters (stal

4条回答
  •  囚心锁ツ
    2020-12-13 10:56

    In my case I needed to serialize and deserialize only one class to XML. For everything else I needed Json. So I registered my adapters like this:

    retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.BASE_URL)
                    .addConverterFactory(EditUserXmlConverterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create(createGson()))
                    .client(httpClient.build())
                    .build();
    

    since I could not extend SimpleXmlConverterFactory (unfortunately) I had to use my own class and change the following line:

    if (!(type instanceof Class)) return null;
    

    to

    if (type != NeedToBeXML.class) return null;
    

    This way only responses and requests of type NeedToBeXML are converted to XML - and everything else JSON.

提交回复
热议问题