I am using Glide 4.11.0 to load image from my localhost with url. I wrote same code for an imageview and recyclerview. I have no problem with imageview, but I get forbidden error (403) when use for recyclerview.
I tried to change header, change wampserver settings, add custom OkHttpUrlLoader, add meta-data, add different implementations, add diskCacheStrategy, skipMemoryCache, override. I know some of them are irrelevant, but I dont know so much thing about that, so I just tried. None of them solve, or I missed up something.
MyViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder{
private ViewDataBinding binding;
public MyViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(final Object obj) {
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
if(obj instanceof Product){
ProductItemBinding productBinding = (ProductItemBinding) binding;
ImageHelper.loadImage(productBinding.imageView, "https://192.168.x.x/"+((Product) obj).getImageUrl());
}
}
}
ImageHelper
public class ImageHelper{
public static void loadImage(final ImageView imageView, String url){
GlideApp.with(imageView.getContext())
.load(url)
.into(imageView);
}
}
AppGlideModule
@GlideModule
public class SimpleAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
RequestOptions options = new RequestOptions();
options.placeholder(R.drawable.ic_launcher_foreground);
builder.setDefaultRequestOptions(options);
}
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
OkHttpClient client = HttpHelper.getUnsafeOkHttpClient();
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
OkHttpClient
public class HttpHelper {
public static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
And implementations:
def glideVersion = '4.11.0'
implementation "com.github.bumptech.glide:glide:$glideVersion"
annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"
annotationProcessor 'androidx.annotation:annotation:1.1.0'
implementation ("com.github.bumptech.glide:okhttp3-integration:$glideVersion")
Here is error. Showns for all items.
W/Glide: Load failed for https://192.168.x.x/product_images/6.jpg
with size [535x221]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 cause:
com.bumptech.glide.load.HttpException(Forbidden)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
There was 1 cause:
com.bumptech.glide.load.HttpException(Forbidden)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
There was 1 cause:
com.bumptech.glide.load.HttpException(Forbidden)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.HttpException: Forbidden
I/Glide: Root cause (1 of 1)
com.bumptech.glide.load.HttpException: Forbidden
at com.bumptech.glide.integration.okhttp3.OkHttpStreamFetcher.onResponse(OkHttpStreamFetcher.java:71)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
And also I run app on my device.
EDIT
I found problem. When I entered urls in database, I pressed enter without realizing. So added new line and the url was wrong..