Home » Android » android – How to load SVG to ImageView using Retrofit + Moshi?-Exceptionshub

android – How to load SVG to ImageView using Retrofit + Moshi?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Using Retrofit + Moshi how can I put SVG into ImageView.

Response from the server:

<svg xmlns="http://www.w3.org/2000/svg" width="33" height="32" viewBox="0 0 33 32" fill="none"><ellipse cx="16.3233" cy="16" rx="16.2874" ry="16" fill="#E6F5F5" /></svg>

Retrofit:

Retrofit.Builder()
        .client(okHttpClient)
        .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())

@Headers("Content-Type: image/svg+xml")
    @GET("url/{id}")
    suspend fun getIcon(
        @Path("id") iconId: UUID
    ): Response<whatever>

Moshi setup:

private val moshi = Moshi
        .Builder()
        .add(KotlinJsonAdapterFactory())
        .build()

Moshi error:

com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was STRING at path

Is it possible with the given response ?

How to&Answers: