I know that HTTP headers can be added like this:
GlideUrl url = new GlideUrl("http://....", new LazyHeaders.Builder()
.addHeader("User-Agent", USER_AGENT)
.addHeader("App-Agent", APP_AGENT)
.build());
....load(url)....;
But how to do that Just one time for all requests?
I would try this way (untested on device, but compiles):
// make sure it's registered in AndroidManifest.xml as described at https://github.com/bumptech/glide/wiki/Configuration#including-a-glidemodule
public class GlideSetup implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) { /* no costumization */ }
@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 static final Headers HEADERS = new LazyHeaders.Builder()
.addHeader("User-Agent", USER_AGENT)
.addHeader("App-Agent", APP_AGENT)
.build();
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) {
return HEADERS;
}
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() { /* nothing to free */ }
}
}
}
and then
....load("http://....")....;
If you're not using load(String), but load(Uri) for example, I think you'll have to register a similar ModelLoader for that too. The idea should be the same for any model.
I guess if you want to combine the two (static and dynamic headers) you would need to change Strings above to GlideUrl and merge headers in getHeaders; or create your own factory for creating Headers which prepopulates a LazyHeaders.Builder for example.
@sjudd was this what you meant in 3.6.0 release notes?
Yup pretty much.
I'd recommend using a custom data model though, rather than String. If you register a BaseGlideUrlLoader<String> like the one you've written that only recognizes URLs, you replace Glide's default String loader that recognizes URLs, Uris, and file paths.
As an alternative you can register a new type (YourModel), register a BaseGlideUrlLoader<YourModel>, and then start loads with objects of that class (Glide.with(fragment).load(yourModel)). You can see the FlickrModelLoader or GiphyModelLoader for examples of this.
I added @TWiStErRob 's codes but not able to get the image.
how can i log glide headers to see what is wrong?
There isn't a great way to log headers. Can you check using a proxy or just server logs to see what you're sending?
You can put a break point in Glide also, if you're using gradle and the standard HttpUrlConnection library (ie you're not depending on an integration library), you can do so here: https://github.com/bumptech/glide/blob/master/library/src/main/java/com/bumptech/glide/load/data/HttpUrlFetcher.java#L91
Let us know if you need more help.
@TWiStErRob Thanks it helped me.
@TWiStErRob Thank you very much! It worked.
@TWiStErRob could you please update your answer to ver 4.6.1? Because methods from current answer don't exist
@Kolyall see above commit
use load(url), not load(uri) or others
Most helpful comment
Yup pretty much.
I'd recommend using a custom data model though, rather than
String. If you register aBaseGlideUrlLoader<String>like the one you've written that only recognizes URLs, you replace Glide's default String loader that recognizes URLs, Uris, and file paths.As an alternative you can register a new type (
YourModel), register aBaseGlideUrlLoader<YourModel>, and then start loads with objects of that class (Glide.with(fragment).load(yourModel)). You can see theFlickrModelLoaderorGiphyModelLoaderfor examples of this.