【问题标题】:Special characters are encoded in POST request in android. How to avoid this?特殊字符编码在 android 的 POST 请求中。如何避免这种情况?
【发布时间】:2022-04-08 20:54:57
【问题描述】:

我正在调用带有一些参数的发布 API。一个参数是

activatecode="aaaaaa$rr"

当 API 调用时,它被发送为

activatecode=aaaaaa%24rr

$ 被编码为 %24。如何避免这种情况并按原样发送特殊字符?

【问题讨论】:

标签: android post retrofit


【解决方案1】:

我正在使用 Retrofit 2.9.0。

我有这项服务:

interface WordpressService {
    @GET("wp-json/wp/v2/posts")
    suspend fun getPosts(
        @Query("page") page: Int,
        @Query("per_page") limit: Int,
        @Query("_fields", encoded = true) fields: String = "date,link,title,content,excerpt,author"
    ): List<Post>
}

没有输入encode = true,我最终得到了这个请求:

GET http://example.org/wp-json/wp/v2/posts?page=1&per_page=10&_fields=date%2Clink%2Ctitle%2Ccontent%2Cexcerpt%2Cauthor

使用encode = true,我得到:

GET http://motspirituel.org/wp-json/wp/v2/posts?page=1&per_page=10&_fields=date,link,title,content,excerpt,author

所以就我而言,在注释中添加encode = true 解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多