1

Using Retrofit converter-gson and converter-simplexml to get response from API in "Content-Type: image/svg+xml"

Retrofit:

Retrofit.Builder()
        .baseUrl(myURL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .addConverterFactory(SimpleXmlConverterFactory.create())
        .build()

Api:

@Headers("Content-Type: image/svg+xml")
@GET("/get-qr-code/{uuid}")
suspend fun getQR(
    @Path("uuid") uuid: String
): String

Error:

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $
    com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

Or if I put addConverterFactory(SimpleXmlConverterFactory.create()) on top of gson converter, I get this error:

Could not deserialize body as class java.lang.String
    java.lang.IllegalStateException: Could not deserialize body as class java.lang.String

And Response from API is:

<?xml version='1.0' encoding='UTF-8'?>
    <svg width="98mm" height="98mm" version="1.1" xmlns="http://www.w3.org/2000/svg"><rect x="8mm" y="8mm" width="2mm" ………. width="2mm" height="2mm"/></svg>

44514-byte body

1 Answers1

0

You can actually use a famous image loading library called Glide. It is capable of loading images with the help of an URL. You can also use AndroidSVG Library for it. See this Answer for more details: Does Glide have a method for loading both PNG and SVG?