Glide使用中遇到的问题 IOException: Request failed with code: 503

报错:
无法访问Fragment
找不到支持android.support.v4.Fragment的类

在build.gradle中添加依赖:

compile 'com.github.bumptech.glide:glide:3.7.0'

原因:glide使用时需要support-v4库的支持,如果你的项目没有support-v4库(项目默认已经添加了),如果被去掉了,则还需要添加support-v4依赖:

compile 'com.android.support:support-v4:23.3.0'  
//注:凡是com.android.support:库-xxx,这样的,库的版本号(xxx)必须保持一致
//例如
//compile 'com.android.support:recyclerview-v7:25.3.1'
//compile 'com.android.support:support-v4:25.3.1'
//recyclerview-v7的版本号是25.3.1,那么support-v4的版本号也得是25.3.1
//这里所说的版本号一致只针对依赖前面是com.android.support: 的

http://blog.csdn.net/shangmingchao/article/details/51125554/

Glide网络加载大图(17M)一直报错
加载方式

public static void loadReceivedImage(Fragment fragment, final String imageUrl, final ImageView imageView)
    {
        Glide.with(fragment)
                .load(imageUrl)
                .thumbnail(0.1f)
                //.placeholder(R.drawable.ic_launcher)
                .listener(requestListener)
                .error(R.drawable.ic_launcher)
                .into(imageView);
    }

    private static RequestListener requestListener = new RequestListener()
    {
        @Override
        public boolean onException(Exception e, String model, Target target, boolean isFirstResource)
        {
            // todo log exception
            Log.error(TAG, "  eeeee=" + e);
            // important to return false so the error placeholder can be placed
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource)
        {
            return false;
        }
    };

报错

java.net.SocketTimeoutException: failed to connect to /192.168.xx.xx (port 80) after 2500ms
java.lang.RuntimeException: java.io.IOException: Failed to read all expected data, expected: 18506912, but read: 18446866
java.io.IOException: Request failed 503: Service Unavailable

IOException: Request failed with code: 503
解决办法:不再加载缩略图(即不再调用.thumbnail(0.5f)),本来发送的图片就是压缩后的图片,这样每个图片只有几十或几百kb,
1、glide图片加载的过小
2、服务端不支持缩略图(使用的临时服务器,hfs文件服务器)
总之不再调用.thumbnail(0.5)后,没有显示(或几乎不报错了)过503问题
原因解决:
参考
glide加载不出大图并报错 java.net.SocketTimeoutException
https://www.cnblogs.com/bylijian/p/6908813.html
https://github.com/bumptech/glide/wiki/Integration-Libraries
https://www.cnblogs.com/bylijian/p/6908813.html

https://www.jianshu.com/p/3ec3ec7c9d33 Glide4.0只播放一次Gif以及监听播放完成的实现方法

一般在Glide3.7的时候是这样解决的,
Glide.with(this)
.load("xxxurl")
.listener(new RequestListener() {
                @Override
                public boolean onException(Exception arg0, Integer arg1,
                        Target arg2, boolean arg3) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource,
                        Integer model, Target target,
                        boolean isFromMemoryCache, boolean isFirstResource) {
                    // 计算动画时长
                    GifDrawable drawable = (GifDrawable) resource;
                    GifDecoder decoder = drawable.getDecoder();
                    for (int i = 0; i < drawable.getFrameCount(); i++) {
                        duration += decoder.getDelay(i);
                    }
                    //发送延时消息,通知动画结束
                    handler.sendEmptyMessageDelayed(MESSAGE_SUCCESS,
                            duration);
                    return false;
                }
            }) //仅仅加载一次gif动画
            .into(new GlideDrawableImageViewTarget(imageview, 1));

在Glide4.0中,没法再直接获取GifDecoder对象了,原因是因为GlideDrawable不再提供这个方法了。 这里是采用反射的方法获取到GifDecoder变量的,具体代码如下:

public static void loadOneTimeGif(Context context, Object model, final ImageView imageView, final GifListener gifListener) {
Glide.with(context).asGif().load(model).listener(new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
return false;
}
        @Override
        public boolean onResourceReady(GifDrawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
            try {
                Field gifStateField = GifDrawable.class.getDeclaredField("state");
                gifStateField.setAccessible(true);
                Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
                Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader");
                gifFrameLoaderField.setAccessible(true);
                Class gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
                Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder");
                gifDecoderField.setAccessible(true);
                Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder");
                Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(resource)));
                Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class);
                getDelayMethod.setAccessible(true);
                //设置只播放一次
                resource.setLoopCount(1);
                //获得总帧数
                int count = resource.getFrameCount();
                int delay = 0;
                for (int i = 0; i < count; i++) {
                    //计算每一帧所需要的时间进行累加
                    delay += (int) getDelayMethod.invoke(gifDecoder, i);
                }
                imageView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (gifListener != null) {
                            gifListener.gifPlayComplete();
                        }
                    }
                }, delay);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return false;
        }
    }).into(imageView);
}

/**
 * Gif播放完毕回调
 */
public interface GifListener {
    void gifPlayComplete();
}

同时,因为我们采用的是反射,所以别忘了在你的proguard-rules.pro中加上Glide的反混淆规则,Glide

-keep class com.bumptech.glide.** {*;}

https://www.jianshu.com/p/a5f1d662e053 Glide缓存汇总
https://blog.csdn.net/m0_37698386/article/details/82660811 Glide缓存导致图片不更新
https://blog.csdn.net/mynameishuangshuai/article/details/51742375 Android严苛模式StrictMode使用详解
https://www.jianshu.com/p/652404b3f775 性能基础:Android6.0 StrictMode 两个新特性与实践
https://blog.csdn.net/xy4_android/article/details/78758146 android 使用Glide加载图片的默认缓存路径

你可能感兴趣的:(glide)