Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
YGOMobile
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fallenstardust
YGOMobile
Commits
940d629b
Commit
940d629b
authored
Sep 28, 2021
by
qq247321453
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
预留下载缓存
parent
875d6e44
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
18 deletions
+32
-18
mobile/src/main/java/cn/garymb/ygomobile/utils/DownloadUtil.java
...src/main/java/cn/garymb/ygomobile/utils/DownloadUtil.java
+32
-18
No files found.
mobile/src/main/java/cn/garymb/ygomobile/utils/DownloadUtil.java
View file @
940d629b
package
cn.garymb.ygomobile.utils
;
import
android.util.Log
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.HashMap
;
import
java.util.Map
;
import
cn.garymb.ygomobile.core.IrrlichtBridge
;
import
okhttp3.Call
;
import
okhttp3.Callback
;
import
okhttp3.OkHttpClient
;
...
...
@@ -15,6 +20,9 @@ public class DownloadUtil {
private
static
DownloadUtil
downloadUtil
;
private
final
OkHttpClient
okHttpClient
;
//暂时关闭
private
static
final
boolean
ENABLE_CACHE
=
false
;
private
static
final
Map
<
String
,
Call
>
cache
=
new
HashMap
<>();
public
static
DownloadUtil
get
()
{
if
(
downloadUtil
==
null
)
{
...
...
@@ -36,7 +44,15 @@ public class DownloadUtil {
*/
public
void
download
(
final
String
url
,
final
String
destFileDir
,
final
String
destFileName
,
final
OnDownloadListener
listener
)
{
if
(
ENABLE_CACHE
){
synchronized
(
cache
){
Call
old
=
cache
.
get
(
url
);
if
(
old
!=
null
){
Log
.
w
(
IrrlichtBridge
.
TAG
,
"exist download task by:"
+
url
);
return
;
}
}
}
Request
request
=
new
Request
.
Builder
()
.
url
(
url
)
.
build
();
...
...
@@ -50,16 +66,21 @@ public class DownloadUtil {
}*/
//异步请求
okHttpClient
.
newCall
(
request
).
enqueue
(
new
Callback
()
{
Call
call
=
okHttpClient
.
newCall
(
request
);
call
.
enqueue
(
new
Callback
()
{
@Override
public
void
onFailure
(
Call
call
,
IOException
e
)
{
// 下载失败监听回调
listener
.
onDownloadFailed
(
e
);
if
(
ENABLE_CACHE
){
synchronized
(
cache
){
cache
.
remove
(
url
);
}
}
}
@Override
public
void
onResponse
(
Call
call
,
Response
response
)
throws
IOException
{
InputStream
is
=
null
;
byte
[]
buf
=
new
byte
[
2048
];
int
len
=
0
;
...
...
@@ -88,24 +109,17 @@ public class DownloadUtil {
fos
.
flush
();
//下载完成
listener
.
onDownloadSuccess
(
file
);
}
catch
(
Exception
e
)
{
listener
.
onDownloadFailed
(
e
);
}
catch
(
Exception
e
x
)
{
listener
.
onDownloadFailed
(
e
x
);
}
finally
{
try
{
if
(
is
!=
null
)
{
is
.
close
();
IOUtils
.
close
(
is
);
IOUtils
.
close
(
fos
);
}
if
(
fos
!=
null
)
{
fos
.
close
();
}
}
catch
(
IOException
e
)
{
if
(
ENABLE_CACHE
){
synchronized
(
cache
){
cache
.
remove
(
url
);
}
}
}
});
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment