错误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.
);
@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())
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);