Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
门心叼龙
MVPArms
提交
80012934
M
MVPArms
项目概览
门心叼龙
/
MVPArms
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MVPArms
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
80012934
编写于
4月 03, 2018
作者:
J
JessYan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve OkHttpStreamFetcher
上级
51fc87c2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
16 addition
and
28 deletion
+16
-28
arms/src/main/java/com/jess/arms/http/OkHttpStreamFetcher.java
...src/main/java/com/jess/arms/http/OkHttpStreamFetcher.java
+16
-28
未找到文件。
arms/src/main/java/com/jess/arms/http/OkHttpStreamFetcher.java
浏览文件 @
80012934
...
...
@@ -15,7 +15,6 @@
*/
package
com.jess.arms.http
;
import
android.os.Build
;
import
android.support.annotation.NonNull
;
import
android.util.Log
;
...
...
@@ -25,7 +24,7 @@ import com.bumptech.glide.load.HttpException;
import
com.bumptech.glide.load.data.DataFetcher
;
import
com.bumptech.glide.load.model.GlideUrl
;
import
com.bumptech.glide.util.ContentLengthInputStream
;
import
com.bumptech.glide.util.
Synthetic
;
import
com.bumptech.glide.util.
Preconditions
;
import
java.io.IOException
;
import
java.io.InputStream
;
...
...
@@ -39,23 +38,27 @@ import okhttp3.ResponseBody;
/**
* Fetches an {@link InputStream} using the okhttp library.
*/
public
class
OkHttpStreamFetcher
implements
DataFetcher
<
InputStream
>,
okhttp3
.
Callback
{
public
class
OkHttpStreamFetcher
implements
DataFetcher
<
InputStream
>,
okhttp3
.
Callback
{
private
static
final
String
TAG
=
"OkHttpFetcher"
;
private
final
Call
.
Factory
client
;
private
final
GlideUrl
url
;
@Synthetic
InputStream
stream
;
@Synthetic
ResponseBody
responseBody
;
private
volatile
Call
call
;
private
InputStream
stream
;
private
ResponseBody
responseBody
;
private
DataCallback
<?
super
InputStream
>
callback
;
// call may be accessed on the main thread while the object is in use on other threads. All other
// accesses to variables may occur on different threads, but only one at a time.
private
volatile
Call
call
;
// Public API.
@SuppressWarnings
(
"WeakerAccess"
)
public
OkHttpStreamFetcher
(
Call
.
Factory
client
,
GlideUrl
url
)
{
this
.
client
=
client
;
this
.
url
=
url
;
}
@Override
public
void
loadData
(
Priority
priority
,
final
DataCallback
<?
super
InputStream
>
callback
)
{
public
void
loadData
(
@NonNull
Priority
priority
,
@NonNull
final
DataCallback
<?
super
InputStream
>
callback
)
{
Request
.
Builder
requestBuilder
=
new
Request
.
Builder
().
url
(
url
.
toStringUrl
());
for
(
Map
.
Entry
<
String
,
String
>
headerEntry
:
url
.
getHeaders
().
entrySet
())
{
String
key
=
headerEntry
.
getKey
();
...
...
@@ -65,25 +68,11 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream>,
this
.
callback
=
callback
;
call
=
client
.
newCall
(
request
);
if
(
Build
.
VERSION
.
SDK_INT
!=
Build
.
VERSION_CODES
.
O
)
{
call
.
enqueue
(
this
);
}
else
{
try
{
// Calling execute instead of enqueue is a workaround for #2355, where okhttp throws a
// ClassCastException on O.
onResponse
(
call
,
call
.
execute
());
}
catch
(
IOException
e
)
{
onFailure
(
call
,
e
);
}
catch
(
ClassCastException
e
)
{
// It's not clear that this catch is necessary, the error may only occur even on O if
// enqueue is used.
onFailure
(
call
,
new
IOException
(
"Workaround for framework bug on O"
,
e
));
}
}
call
.
enqueue
(
this
);
}
@Override
public
void
onFailure
(
Call
call
,
IOException
e
)
{
public
void
onFailure
(
@NonNull
Call
call
,
@NonNull
IOException
e
)
{
if
(
Log
.
isLoggable
(
TAG
,
Log
.
DEBUG
))
{
Log
.
d
(
TAG
,
"OkHttp failed to obtain result"
,
e
);
}
...
...
@@ -92,10 +81,10 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream>,
}
@Override
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
public
void
onResponse
(
@NonNull
Call
call
,
@NonNull
Response
response
)
{
responseBody
=
response
.
body
();
if
(
response
.
isSuccessful
())
{
long
contentLength
=
responseBody
.
contentLength
();
long
contentLength
=
Preconditions
.
checkNotNull
(
responseBody
)
.
contentLength
();
stream
=
ContentLengthInputStream
.
obtain
(
responseBody
.
byteStream
(),
contentLength
);
callback
.
onDataReady
(
stream
);
}
else
{
...
...
@@ -137,5 +126,4 @@ public class OkHttpStreamFetcher implements DataFetcher<InputStream>,
public
DataSource
getDataSource
()
{
return
DataSource
.
REMOTE
;
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录