如何发布JSON,但使用Retrofit2获取XML响应?

问题描述:

从服务器发送Json,但Retrofit2响应是xml。如何发布JSON,但使用Retrofit2获取XML响应?

我在此链接中找到了解决方案。 retrofit solution

但是,这种解决方案是retrof不改造2。

在问题中引用的问题的this answer中使用带有JSON和XML转换器的自定义转换器。然后使用该转换器创建Retrofit实例。

Retrofit retrofit = new Retrofit.Builder().baseUrl(BASE_URL) 
     .addConverterFactory(new MixedConverter(new SimpleXMLConverter(), GsonConverterFactory.create())) 
     .build(); 
ApiService apiService = retrofit.create(ApiService.class); 

使用POJO Generator Plugin自定义转换器responseJSONXML,有了它,你可以用XMLresponse

使用JSON

.setConverter(new MixedConverter(new SimpleXMLConverter(), new GsonConverter(gson))); 

并用于this