如何在Android中添加多个头的改造响应

xpcnnkqh  于 2023-02-10  发布在  Android
关注(0)|答案(4)|浏览(127)

在我的应用程序中,我想从服务器获得一些数据,为此我使用了翻新
我应该添加一些标题,但运行应用程序后显示ForceClose错误!

    • ApiServices代码:**
interface ApiServices {
    @Headers(
        "Accept: application/json",
        "Content-Type : application/json"
    )
    @POST("user/get-password")
    fun sendPhoneNumber(@Body body: LoginSendPhoneBody): Single<Response<LoginSendPhoneResponse>>

    @Headers(
        "Accept: application/json",
        "Content-Type : application/json"
    )
    @POST("addresses")
    fun newAddress(@Header("Authorization") userToken: String, @Body body: NewAddressBody):
            Single<Response<NewAddressResponse>>
}

我的改型版本是2.5.0!
在我阅读的设置多个标题改型文档中,我应该将{}用于标题,例如@Headers({...}),但添加{}后显示错误,我无法使用{}!

    • 日志目录错误:**
java.lang.IllegalArgumentException: Unexpected char 0x20 at 12 in header name: Content-Type 
    at okhttp3.Headers.checkName(Headers.java:261)
    at okhttp3.Headers$Builder.add(Headers.java:311)
    at retrofit2.RequestFactory$Builder.parseHeaders(RequestFactory.java:283)
    at retrofit2.RequestFactory$Builder.parseMethodAnnotation(RequestFactory.java:224)
    at retrofit2.RequestFactory$Builder.build(RequestFactory.java:161)
    at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
    at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
    at java.lang.reflect.Proxy.invoke(Proxy.java:913)
    at $Proxy2.sendPhoneNumber(Unknown Source)
    at com.app.app.data.network.ApiUseCase.getSendPhoneNumber(ApiUseCase.kt:42)

我该怎么修呢?

eqfvzcg8

eqfvzcg81#

改变

"Content-Type : application/json"

"Content-Type: application/json"

只需删除Content-Type之后的空白:)

sbdsn5lh

sbdsn5lh2#

您可以使用标题Map,相同

val header = HashMap<String, String>()
    header["Accept"] = "application/json"
    header["Content-Type"] = "application/json"
    header["Authorization"] = "userToken"


@POST("addresses")
    fun newAddress(@HeaderMap headers: Map<String, String>, @Body body: NewAddressBody): Single<Response<NewAddressResponse>>
svgewumm

svgewumm3#

在KotlinAPI接口中:

@Headers(
        "Accept: application/json",
        "User-Agent : PostmanRuntime/7.29.0",
        "Accept : */*",
        "Accept-Encoding : gzip, deflate, br",
        "Connection : keep-alive"
    )
rslzwgfq

rslzwgfq4#

你可以这样使用你的方法:

fun callLoginAPI(@HeaderMap header: Map<String, String>)

调用此方法时,只需创建Map并添加所有标头

相关问题