【问题标题】:How to load SVG to ImageView using Retrofit + Moshi?如何使用 Retrofit + Moshi 将 SVG 加载到 ImageView?
【发布时间】:2020-06-06 00:30:47
【问题描述】:

使用 Retrofit + Moshi 如何将 SVG 放入 ImageView。

来自服务器的响应:

<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.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 设置:

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

Moshi 错误:

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

给定的响应是否可能?

【问题讨论】:

  • 添加整个异常和您要反序列化响应的模型类。
  • 这是整个消息,模型是什么,可以是字符串

标签: android svg retrofit moshi


【解决方案1】:

错误消息清楚地表明您将类型指定为对象(响应)但收到了字符串。我假设将返回类型更改为 String 应该可以工作

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

或者只是

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

【讨论】:

    猜你喜欢
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多