Glide 网络请求
来源:互联网 发布:广东元擎网络培训骗局 编辑:程序博客网 时间:2024/06/05 01:51
- Glide 内部使用 HttpURLConnection 执行网络请求
public class HttpUrlFetcher implements DataFetcher<InputStream> {}private InputStream loadDataWithRedirects(URL url, int redirects, URL lastUrl, Map<String, String> headers) throws IOException { if (redirects >= MAXIMUM_REDIRECTS) { throw new HttpException("Too many (> " + MAXIMUM_REDIRECTS + ") redirects!"); } else { // Comparing the URLs using .equals performs additional network I/O and is generally broken. // See http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html. try { if (lastUrl != null && url.toURI().equals(lastUrl.toURI())) { throw new HttpException("In re-direct loop"); } } catch (URISyntaxException e) { // Do nothing, this is best effort. } } urlConnection = connectionFactory.build(url); for (Map.Entry<String, String> headerEntry : headers.entrySet()) { urlConnection.addRequestProperty(headerEntry.getKey(), headerEntry.getValue()); } urlConnection.setConnectTimeout(timeout); urlConnection.setReadTimeout(timeout); urlConnection.setUseCaches(false); urlConnection.setDoInput(true); // Stop the urlConnection instance of HttpUrlConnection from following redirects so that // redirects will be handled by recursive calls to this method, loadDataWithRedirects. urlConnection.setInstanceFollowRedirects(false); // Connect explicitly to avoid errors in decoders if connection fails. urlConnection.connect(); // Set the stream so that it's closed in cleanup to avoid resource leaks. See #2352. stream = urlConnection.getInputStream(); if (isCancelled) { return null; } final int statusCode = urlConnection.getResponseCode(); if (statusCode / 100 == 2) { return getStreamForSuccessfulRequest(urlConnection); } else if (statusCode / 100 == 3) { String redirectUrlString = urlConnection.getHeaderField("Location"); if (TextUtils.isEmpty(redirectUrlString)) { throw new HttpException("Received empty or null redirect url"); } URL redirectUrl = new URL(url, redirectUrlString); // Closing the stream specifically is required to avoid leaking ResponseBodys in addition // to disconnecting the url connection below. See #2352. cleanup(); return loadDataWithRedirects(redirectUrl, redirects + 1, url, headers); } else if (statusCode == -1) { throw new HttpException(statusCode); } else { throw new HttpException(urlConnection.getResponseMessage(), statusCode); } }
- Glide 的线程池
public final class GlideExecutor extends ThreadPoolExecutor {}GlideExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTimeInMs, String name, UncaughtThrowableStrategy uncaughtThrowableStrategy, boolean preventNetworkOperations, boolean executeSynchronously, BlockingQueue<Runnable> queue) { super( corePoolSize, maximumPoolSize, keepAliveTimeInMs, TimeUnit.MILLISECONDS, queue, new DefaultThreadFactory(name, uncaughtThrowableStrategy, preventNetworkOperations)); this.executeSynchronously = executeSynchronously; } private static final class DefaultThreadFactory implements ThreadFactory { private final String name; @Synthetic final UncaughtThrowableStrategy uncaughtThrowableStrategy; @Synthetic final boolean preventNetworkOperations; private int threadNum; DefaultThreadFactory(String name, UncaughtThrowableStrategy uncaughtThrowableStrategy, boolean preventNetworkOperations) { this.name = name; this.uncaughtThrowableStrategy = uncaughtThrowableStrategy; this.preventNetworkOperations = preventNetworkOperations; } @Override public synchronized Thread newThread(@NonNull Runnable runnable) { final Thread result = new Thread(runnable, "glide-" + name + "-thread-" + threadNum) { @Override public void run() { android.os.Process.setThreadPriority( android.os.Process.THREAD_PRIORITY_BACKGROUND + android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE); if (preventNetworkOperations) { StrictMode.setThreadPolicy( new ThreadPolicy.Builder() .detectNetwork() .penaltyDeath() .build()); } try { super.run(); } catch (Throwable t) { uncaughtThrowableStrategy.handle(t); } } }; threadNum++; return result; } }}
- 数据回调
interface DataCallback<T> { void onDataReady(@Nullable T data); void onLoadFailed(Exception e); }
链接:http://blog.csdn.net/u013510838/article/details/52143113
阅读全文
0 0
- Glide 网络请求
- Android Glide切换网络请求方式
- Glide源码分析4 -- 缓存,编解码和网络请求
- Glide的缓存和网络请求方式的配置
- recyclerview+okhttp+glide实现网络请求加载图片列表
- okhttp + RecycleView + Glide 请求网络数据及图片
- Glide 4.x之请求网络图片数据流程解析
- glide添加header请求
- glide:请求优先级
- 网络请求库(ion、volley、retrofit)和图片加载库(glide、picasso、fresco)
- Android使用OKHTTP网络框架请求数据,RecyclerView结合Glide展示图片,瀑布流布局样式
- SSS___listview多条目加载、使用okhttp请求网络数据,Glide加载图片
- Glide+Banner网络请求加载图片实现无限自动轮播+小圆点
- Kotlin新语言使用RxJava+Retrofit请求网络数据+lambda表达式+RecyclerView展示+Glide展示图片
- 网络框架之Glide
- glide:整合网络协议栈
- Glide 图片请求库(工具类)
- Glide入门教程——8. 请求优先级
- Python:python中math模块中提供的基本数学函数
- 华为余承东给新员工的讲话
- jQuery基本用法一
- php导出excel格式的数据(不利于插件)
- springmvc国际化+上传+下载
- Glide 网络请求
- webpack/webpack+bundle-loader/webpack+redux code splitting(按需加载)
- Ubuntu上出现无法获得锁“Could not get lock /var/lib/apt/lists/lock”问题的解决
- .project文件
- Max Script 入门教程
- 从2013到2017 CGU 草根战队的电竞梦
- 春花秋月何时了 MVP模式知多少
- 算法导论B树的c++实现
- 25年的思考沉淀,只为成就这一款经典