Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在改装2.0中使用授权令牌=A-123456789qwertyuio12标头_Android_Rest_Retrofit2 - Fatal编程技术网

Android 如何在改装2.0中使用授权令牌=A-123456789qwertyuio12标头

Android 如何在改装2.0中使用授权令牌=A-123456789qwertyuio12标头,android,rest,retrofit2,Android,Rest,Retrofit2,我正在尝试使用一个有授权标题的api,我可以在Postman中得到一个包含所有数据的200响应,但无法使其在改型中工作 @GET("your server url goes here") Call<Your_Model_Class> getServerData(@Header("Authorization") String token); @GET(“您的服务器url位于此处”) 调用getServerData(@Header(“Authorization”)字符串令牌);

我正在尝试使用一个有授权标题的api,我可以在Postman中得到一个包含所有数据的200响应,但无法使其在改型中工作

@GET("your server url goes here")
    Call<Your_Model_Class> getServerData(@Header("Authorization") String token);
@GET(“您的服务器url位于此处”)
调用getServerData(@Header(“Authorization”)字符串令牌);

将您的令牌传递给
getServerData
方法。

可能需要使用OkHttp拦截器添加
令牌

OkHttpClient client = new OkHttpClient.Builder()
        .addNetworkInterceptor(mTokenInterceptor)
        .build();
然后将其添加到改装中:

Retrofit retrofit = new Retrofit.Builder()
        .client(client)
        .baseUrl(base_url)
        .build();
mtokeinterceptor

Interceptor mTokenInterceptor = new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if (mToken != null) {
            Request.Builder requestBuilder = request.newBuilder()
                    .addHeader("Authorization", mToken);
            Request newRequest = requestBuilder.build();

            return chain.proceed(newRequest);
        }
        return chain.proceed(request);

    }
};

当您获得
令牌
时,只需分配
mToken

显示您的代码,就像您迄今为止尝试的那样