简体   繁体   中英

How to convert image/svg+xml from API with retrofit 2

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM