即使设置了授权令牌,也会出现错误401

问题描述 投票:0回答:2

错误401。尝试使用@POST访问api,并且已经设置了@Header(“ Authorization”)字符串令牌。

我已经使用@GET尝试过,并且可以正常工作,但是我传递了一些表单字段,所以我需要使用@POST

 @POST("details")
 @FormUrlEncoded
 Call<Play> playTrack(
    @Header("Authorization") String TOKEN,
    @Field("event_id") int event_id,
    @Field("longitude") double longitude,
    @Field("latitude") double latitude
//The above is the code.





    );
android authorization retrofit
2个回答
0
投票
尝试在批注上使用多部分

@Multipart @POST("details") Call<ResponseBody> playTrack( @Part("event_id") RequestBody eventId, @Part("longitude") RequestBody longitude, @Part("latitude") RequestBody latitude, );

确保将RequestBody作为参数传递

val latitude = RequestBody.create(MediaType.parse("text/plain"), doubleLatitude.toString())


0
投票
尝试创建标头拦截器并将其添加到OkHttpClient:

Interceptor headerIntercepter = new Interceptor() { @Override public okhttp3.Response intercept(Chain chain) throws IOException { return chain.proceed( chain.request().newBuilder().addHeader("authorization- client", accessToken).build()); } }; OkHttpClient client = new OkHttpClient.Builder().connectTimeout(120, TimeUnit.SECONDS).readTimeout(120, TimeUnit.SECONDS) .addInterceptor(headerIntercepter) .build(); try { Retrofit retrofit = new Retrofit.Builder() .baseUrl(Server_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .build(); apiService = retrofit.create(Api.class);

© www.soinside.com 2019 - 2024. All rights reserved.