安卓listview或Recycle中防止图片被抓包

主要针对图片地址防止被抓到,隐藏自己真实服务器地址。网络请求使用的是OKhttp,图片加载使用的是glide。

1、

proxy(Proxy.NO_PROXY)加上这个,让代理不能抓取真实链接。在全局application中配置

OkHttpClient okHttpClient = new OkHttpClient.Builder()
      .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
      .proxy(Proxy.NO_PROXY)
      //.addInterceptor(new LoggerInterceptor(LogCommonUtil.TAG))
      .connectTimeout(20000L, TimeUnit.MILLISECONDS)
      .readTimeout(20000L, TimeUnit.MILLISECONDS)
      //其他配置
      .build();
OkHttpUtils.initClient(okHttpClient);

2、

在adapter中判断文件是否已经下载过了,如果下载过,就直接去内存中加载图片。

①、获取listview的数据okhttp获取,不用多加什么已经可以防止抓包,注意版本,我用的是

compile 'com.zhy:okhttputils:2.6.2'(应该是比这版本高才行)

②、先用okhttp下载图片到本地,保存为file。然后用glide加载。判断如果保存过就直接用glide加载文件
//判断文件是否存在
//下面的地址可以更换。但是一定要用get,post会报405,不知道为啥,调试了两个小时才发现这个问题,有会的求教。
if(!fileIsExists(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + list.get(pos).getLenderLogo())){
    OkHttpUtils.get().url(imagePrefix + list.get(pos).getLenderLogo()).build().execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), list.get(pos).getLenderLogo()) {
        @Override
        public void onError(Call call, Exception e, int id) {
            Log.e(TAG, "onError :" + e.getMessage());
        }

        @Override
        public void inProgress(float progress, long total, int id) {
            Log.e(TAG,"inProgress"+(int)(100*progress));
        }

        @Override
        public void onResponse(File file, int id) {
            Glide.with(UIUtils.getContext()).load(file).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.default_image).into(holder.third_item_image);
        }
    });
}else {
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + list.get(pos).getLenderLogo());
    Glide.with(UIUtils.getContext()).load(file).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.default_image).into(holder.third_item_image);
}

//判断文件是否存在
public boolean fileIsExists(String strFile) {
    try {
        File f=new File(strFile);
        if(!f.exists()) {
            return false;
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}

3、

欢迎留言交流。

发布了56 篇原创文章 · 获赞 12 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/huwan12345/article/details/78502278