如何将Authorization标头添加到所有Glide 4.x请求

时间:2017-08-20 13:30:15

标签: android android-glide

在Glide 3.x中,我们可以添加一个String模型加载器,如下所示:

public class GlideService /* implements GlideModule*/ {


    @Override
    public void registerComponents(Context context, Glide glide) {
        glide.register(String.class, InputStream.class, new HeaderedLoader.Factory());
    }

    private static class HeaderedLoader extends BaseGlideUrlLoader<String> {

        public HeaderedLoader(Context context) {
            super(context);
        }

        @Override
        protected String getUrl(String model, int width, int height) {
            return model;
        }


        @Override
        protected Headers getHeaders(String model, int width, int height) {

            LazyHeaders.Builder headersBuilder = new LazyHeaders.Builder();
            if (BuildConfig.FLAVOR.equals("staging")) {
                String auth = "username:password";
                String base64 = Base64.encodeToString(auth.getBytes(), Base64.NO_WRAP);
                headersBuilder.addHeader("Authorization", "Basic " + base64);
            }

            return headersBuilder.build();
        }

        public static class Factory implements ModelLoaderFactory<String, InputStream> {
            @Override
            public StreamModelLoader<String> build(Context context, GenericLoaderFactory factories) {
                return new HeaderedLoader(context);
            }

            @Override
            public void teardown() { }
        }
    }

}

但是从文档中我们不清楚我们如何才能在最新版本中实现这一目标。

2 个答案:

答案 0 :(得分:3)

我面临与你相同的问题。 以下是我修复它的方法。

  1. 您的.compareTo()需要扩展GlideService(如果您正在为应用程序编写代码)。有关详细信息,请参阅glide documentation

  2. 正如您现在使用滑翔4.x一样,您的班级AppGlideModule现已覆盖HeaderedLoader的两个默认构造函数,这些构造函数为BaseGlideUrlLoader<String>protected HeaderedLoader(ModelLoader<GlideUrl, InputStream> concreteLoader)

  3. protected HeaderedLoader(ModelLoader<GlideUrl, InputStream> concreteLoader, @Nullable ModelCache<String, GlideUrl> modelCache)课程创建工厂。 这看起来应该是

    HeaderedLoader
  4. 然后,您将使用以下

    覆盖滑翔模块超类的static class Factory implements ModelLoaderFactory<String, InputStream> { @Override public ModelLoader<String, InputStream> build(MultiModelLoaderFactory multiFactory) { ModelLoader<GlideUrl, InputStream> loader = multiFactory.build(GlideUrl.class, InputStream.class); return new HeaderedLoader(loader); } @Override public void teardown() { /* nothing to free */ } } 方法
    public void registerComponents(Context context, Glide glide, Registry registry)
  5. 您应该能够保留在@Override public void registerComponents(Context context, Glide glide, Registry registry) { registry.append(String.class, InputStream.class, new HeaderedLoader.Factory()); } 类中处理http标头的逻辑。 我已经使用这种方法为glide 4.0添加自定义标头滑动请求。

答案 1 :(得分:1)

试试这个

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.OkHttpClient;

/**
 * A simple model loader for fetching media over http/https using OkHttp.
 */
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {

  private final Call.Factory client;

  public OkHttpUrlLoader(Call.Factory client) {
    this.client = client;
  }

  @Override
  public boolean handles(GlideUrl url) {
    return true;
  }

  @Override
  public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height,
      Options options) {
    return new LoadData<>(model, new OkHttpStreamFetcher(client, model));
  }

  /**
   * The default factory for {@link OkHttpUrlLoader}s.
   */
  public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
    private static volatile Call.Factory internalClient;
    private Call.Factory client;

    private static Call.Factory getInternalClient() {
      if (internalClient == null) {
        synchronized (Factory.class) {
          if (internalClient == null) {
            internalClient = new OkHttpClient();
          }
        }
      }
      return internalClient;
    }

    /**
     * Constructor for a new Factory that runs requests using a static singleton client.
     */
    public Factory() {
      this(getInternalClient());
    }

    /**
     * Constructor for a new Factory that runs requests using given client.
     *
     * @param client this is typically an instance of {@code OkHttpClient}.
     */
    public Factory(Call.Factory client) {
      this.client = client;
    }

    @Override
    public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
      return new OkHttpUrlLoader(client);
    }

    @Override
    public void teardown() {
      // Do nothing, this instance doesn't own the client.
    }
  }
}`