代码之家  ›  专栏  ›  技术社区  ›  byteC0de

如何在MVP模式中向报头添加访问令牌

  •  -1
  • byteC0de  · 技术社区  · 8 年前

    我正在使用 MVP 我的android应用程序模式,我需要添加访问令牌到我的请求头。访问令牌保存在 SharedPreferences . 如何访问它 共享引用 最有价值球员 图案。我正在使用 Retrofit 用于网络请求。

    public class RetrofitInstance {
    
        private static Retrofit retrofit;
        private static final String BASE_URL = "http://123124.ngrok.io/api/";
    
    
        public static Retrofit getRetrofitInstance() {
            if (retrofit == null) {
    
                OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
                okhttpBuilder.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
    
                        Request request = chain.request();
                        Request.Builder newRequest = request.newBuilder().addHeader("Authorization", "Bearer "); //need to add value from SharedPreferences
    
                        return chain.proceed(newRequest.build());
                    }
                });
    
                retrofit = new retrofit2.Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(okhttpBuilder.build())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }
    
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Nabil    8 年前

    尝试:

    private static Retrofit authRetrofit = null;        
    
    public static Retrofit getAuthClient(Context context) {
                        if (authRetrofit == null) {
                            final AuthSharedPref authSharedPref = new AuthSharedPref(context);
                            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
                            httpClient.addInterceptor(new Interceptor() {
                                @Override
                                public Response intercept(@NonNull Chain chain) throws IOException {
                                    Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer "+authSharedPref.getToken()).build();
                                    return chain.proceed(request);
                                }
                            });
    
                          authRetrofit= new retrofit2.Retrofit.Builder()
                         .baseUrl(BASE_URL)
                         .client(okhttpBuilder.build())
                         .addConverterFactory(GsonConverterFactory.create())
                         .build();
                        }
                        return authRetrofit;
    }
    

    在这里,AuthSharedPref是一个共享首选项类,用于存储登录详细信息,您可以将其更改为自己的登录详细信息。

        2
  •  1
  •   Manohar    8 年前

    Context

    这样试试

      public class RetrofitInstance {
    
        private static Retrofit retrofit;
        private Context context;
        private static final String BASE_URL = "http://123124.ngrok.io/api/";
    
         public void init(Context context) {
              this.context = context;
         }
    
        public static Retrofit getRetrofitInstance() {
            if (retrofit == null) {
    
                OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
                okhttpBuilder.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
    
                        Request request = chain.request();
                        //use context
                        Request.Builder newRequest = request.newBuilder().addHeader("Authorization", "Bearer "); //need to add value from SharedPreferences
    
                        return chain.proceed(newRequest.build());
                    }
                });
    
                retrofit = new retrofit2.Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(okhttpBuilder.build())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
            }
            return retrofit;
        }
    
    }
    

    public class YourApplication extends Application {
    
     @Override
        public void onCreate() {
            super.onCreate();
             RetrofitInstance.getRetrofitInstance().init(getApplicationContext());
             }
       }