package com.example.glideprogresstest;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
/**
* @author guolin
* @since 2017/11/2
*/
public class OkHttpFetcher implements DataFetcher<InputStream> {
private final OkHttpClient client;
private final GlideUrl url;
private InputStream stream;
private ResponseBody responseBody;
private volatile boolean isCancelled;
public OkHttpFetcher(OkHttpClient client, GlideUrl url) {
this.client = client;
this.url = url;
}
@Override
public InputStream loadData(Priority priority) throws Exception {
Request.Builder requestBuilder = new Request.Builder()
.url(url.toStringUrl());
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
Request request = requestBuilder.build();
if (isCancelled) {
return null;
}
Response response = client.newCall(request).execute();
responseBody = response.body();
if (!response.isSuccessful() || responseBody == null) {
throw new IOException("Request failed with code: " + response.code());
}
stream = ContentLengthInputStream.obtain(responseBody.byteStream(),
responseBody.contentLength());
return stream;
}
@Override
public void cleanup() {
try {
if (stream != null) {
stream.close();
}
if (responseBody != null) {
responseBody.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public String getId() {
return url.getCacheKey();
}
@Override
public void cancel() {
isCancelled = true;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
GlideProgressTest.zip (91个子文件)
GlideProgressTest
.gitignore 118B
gradle
wrapper
gradle-wrapper.jar 52KB
gradle-wrapper.properties 230B
.DS_Store 6KB
local.properties 435B
build.gradle 369B
gradlew.bat 2KB
gradlew 5KB
app
.gitignore 7B
build.gradle 777B
proguard-rules.pro 751B
app.iml 9KB
libs
src
test
java
com
example
glideprogresstest
ExampleUnitTest.java 407B
main
AndroidManifest.xml 926B
java
com
example
glideprogresstest
ProgressInterceptor.java 1KB
ProgressResponseBody.java 2KB
ProgressListener.java 331B
MainActivity.java 2KB
OkHttpFetcher.java 2KB
OkHttpGlideUrlLoader.java 2KB
MyGlideModule.java 863B
res
values
styles.xml 383B
colors.xml 208B
strings.xml 80B
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
layout
activity_main.xml 569B
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
drawable
ic_launcher_background.xml 5KB
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
androidTest
java
com
example
glideprogresstest
ExampleInstrumentedTest.java 759B
.DS_Store 6KB
.gradle
4.1
fileChanges
last-build.bin 1B
taskHistory
fileSnapshots.bin 1.97MB
taskHistory.bin 84KB
taskHistory.lock 17B
javaCompile
taskJars.bin 21KB
classAnalysis.bin 997KB
taskHistory.bin 110KB
jarAnalysis.bin 414KB
javaCompile.lock 17B
fileContent
fileContent.lock 17B
fileHashes
fileHashes.lock 17B
resourceHashesCache.bin 20KB
fileHashes.bin 272KB
3.3
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.lock 17B
localClassSetAnalysis.bin 84KB
taskArtifacts
fileSnapshots.bin 3.35MB
taskArtifacts.lock 17B
fileHashes.bin 275KB
taskArtifacts.bin 129KB
buildOutputCleanup
cache.properties 49B
built.bin 0B
cache.properties.lock 2B
.DS_Store 6KB
gradle.properties 730B
.idea
libraries
glide_3_7_0.xml 512B
support_compat_25_3_1.xml 577B
support_fragment_25_3_1.xml 583B
animated_vector_drawable_25_3_1.xml 607B
okhttp_3_9_0.xml 506B
support_vector_drawable_25_3_1.xml 604B
support_media_compat_25_3_1.xml 595B
okio_1_13_0.xml 496B
appcompat_v7_25_3_1.xml 571B
support_core_utils_25_3_1.xml 589B
support_v4_25_3_1.xml 395B
support_annotations_25_3_1.xml 519B
support_core_ui_25_3_1.xml 580B
runConfigurations.xml 564B
workspace.xml 181KB
misc.xml 2KB
modules.xml 371B
copyright
profiles_settings.xml 74B
compiler.xml 686B
gradle.xml 626B
.DS_Store 6KB
GlideProgressTest.iml 894B
.DS_Store 6KB
settings.gradle 15B
共 91 条
- 1
guolin
- 粉丝: 8w+
- 资源: 45
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 台达DOP系列触摸屏与DVP系列PLC通信电缆连接手册-20160122.pdf
- 计算机二级考试心得体会
- 织梦同步WAP插件(V1.4)
- linux之centos7打包与压缩命令详解
- (资源包名是松下不必介意实际是台达)台达PLC例程源码板式家具封边机
- (资源包名是松下不必介意实际是台达)台达PLC例程源码PLS交替输出(单按钮启停)
- (资源包名是松下不必介意实际是台达)台达PLC例程源码PLF指令(车库红绿灯控制)
- 织梦DedeCMS文章归档插件UTF-8和GBK版本
- (资源包名是松下不必介意实际是台达)台达PLC例程源码PLC与6台VFD-B的通讯
- doublescreen,MAXhub会议系统软件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页