Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
android
/
dayu
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
b7e10718
authored
Feb 05, 2018
by
罗翻
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
downloadApi修改
parent
596e143c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
72 deletions
app/src/main/java/com/dayu/bigfish/api/Api.java
app/src/main/java/com/dayu/bigfish/utils/managers/DownloadManager.java
app/src/main/java/com/dayu/bigfish/api/Api.java
View file @
b7e10718
package
com
.
dayu
.
bigfish
.
api
;
import
android.support.annotation.NonNull
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
com.dayu.bigfish.Constants
;
import
com.dayu.bigfish.base.BaseResponse
;
...
...
@@ -32,106 +30,70 @@ import retrofit2.converter.gson.GsonConverterFactory;
* Created by luofan on 2017/11/09.
*/
public
class
Api
{
private
static
APIService
service
;
private
static
Retrofit
retrofit
;
private
static
final
int
DEFAULT_TIMEOUT
=
60
;
private
static
Retrofit
downloadRetrofit
;
/**
*普通retrofit.
* @return
*/
public
static
APIService
getService
()
{
service
=
getRetrofit
().
create
(
APIService
.
class
);
return
service
;
return
getRetrofit
().
create
(
APIService
.
class
);
}
public
static
<
T
>
T
getService
(
Class
<
T
>
clz
)
{
return
getRetrofit
().
create
(
clz
);
/**
* 下载的retrofit.
* @return
*/
public
static
APIService
getDownloadService
()
{
return
getDownloadRetrofit
().
create
(
APIService
.
class
);
}
private
static
Retrofit
getRetrofit
()
{
if
(
retrofit
==
null
)
{
Gson
gson
=
new
GsonBuilder
()
.
setDateFormat
(
"yyyy-MM-dd'T'HH:mm:ssZ"
)
.
create
();
HttpLoggingInterceptor
httpLoggingInterceptor
=
new
HttpLoggingInterceptor
(
m
->
LogUtils
.
i
(
"request"
,
m
));
httpLoggingInterceptor
.
setLevel
(
HttpLoggingInterceptor
.
Level
.
BODY
);
TokenInterceptord
tokenInterceptord
=
new
TokenInterceptord
();
OkHttpClient
okHttpClient
=
new
OkHttpClient
.
Builder
()
.
addInterceptor
(
httpLoggingInterceptor
)
.
addInterceptor
(
tokenInterceptord
)
.
readTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
)
.
writeTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
)
.
connectTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
)
.
build
();
retrofit
=
new
Retrofit
.
Builder
()
.
client
(
okHttpClient
)
.
baseUrl
(
Constants
.
BASE_URL
)
.
addConverterFactory
(
GsonConverterFactory
.
create
(
gson
))
.
addCallAdapterFactory
(
RxJava2CallAdapterFactory
.
create
())
.
build
();
retrofit
=
getRetrofit
(
""
);
}
return
retrofit
;
}
p
ublic
static
APIService
getDownloadRetrofit
()
{
p
rivate
static
Retrofit
getDownloadRetrofit
()
{
if
(
downloadRetrofit
==
null
)
{
downloadRetrofit
=
getRetrofit
(
"download"
);
}
return
downloadRetrofit
;
}
private
static
Retrofit
getRetrofit
(
String
type
)
{
Gson
gson
=
new
GsonBuilder
()
.
setDateFormat
(
"yyyy-MM-dd'T'HH:mm:ssZ
"
)
.
setDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss
"
)
.
create
();
HttpLoggingInterceptor
httpLoggingInterceptor
=
new
HttpLoggingInterceptor
(
m
->
Log
.
i
(
"retrofi
t"
,
m
));
HttpLoggingInterceptor
httpLoggingInterceptor
=
new
HttpLoggingInterceptor
(
m
->
LogUtils
.
i
(
"reques
t"
,
m
));
httpLoggingInterceptor
.
setLevel
(
HttpLoggingInterceptor
.
Level
.
BODY
);
OkHttpClient
okHttpClient
=
new
OkHttpClient
.
Builder
()
TokenInterceptord
tokenInterceptord
=
new
TokenInterceptord
();
OkHttpClient
.
Builder
build
=
new
OkHttpClient
.
Builder
()
.
addInterceptor
(
httpLoggingInterceptor
)
.
connectTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
)
.
readTimeout
(
600
,
TimeUnit
.
SECONDS
)
.
writeTimeout
(
600
,
TimeUnit
.
SECONDS
)
.
addNetworkInterceptor
(
new
Interceptor
()
{
@Override
public
Response
intercept
(
@NonNull
Chain
chain
)
throws
IOException
{
.
addInterceptor
(
tokenInterceptord
)
.
readTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
)
.
writeTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
)
.
connectTimeout
(
DEFAULT_TIMEOUT
,
TimeUnit
.
SECONDS
);
if
(
"download"
.
equals
(
type
))
{
build
.
addNetworkInterceptor
(
chain
->
{
Response
response
=
chain
.
proceed
(
chain
.
request
());
return
response
.
newBuilder
()
.
body
(
new
FileResponseBody
(
response
.
body
()))
//将自定义的ResposeBody设置给它
.
build
();
});
}
})
.
build
();
downloadRetrofit
=
new
Retrofit
.
Builder
()
.
client
(
okHttpClient
)
return
new
Retrofit
.
Builder
()
.
client
(
build
.
build
())
.
baseUrl
(
Constants
.
BASE_URL
)
.
addConverterFactory
(
GsonConverterFactory
.
create
(
gson
))
.
addCallAdapterFactory
(
RxJava2CallAdapterFactory
.
create
())
.
build
();
}
return
downloadRetrofit
.
create
(
APIService
.
class
);
}
/**
* 对网络接口返回的Response进行分割操作
*
* @param response
* @param <T>
* @return
*/
public
static
<
T
>
Observable
<
T
>
flatResponse
(
final
BaseResponse
<
T
>
response
)
{
return
Observable
.
create
(
e
->
{
if
(
response
.
isSuccess
())
{
if
(!
e
.
isDisposed
())
{
e
.
onNext
(
response
.
getData
());
}
}
else
{
if
(!
e
.
isDisposed
())
{
e
.
onError
(
new
ServerException
(
response
.
getSubCode
(),
response
.
getMsg
()));
}
return
;
}
if
(!
e
.
isDisposed
())
{
e
.
onComplete
();
}
});
}
/**
* 统一加上token.
*/
...
...
@@ -159,6 +121,7 @@ public class Api {
return
(
ObservableTransformer
<
BaseResponse
<
T
>,
T
>)
transformer
;
}
final
static
ObservableTransformer
transformer
=
new
ObservableTransformer
()
{
@Override
public
ObservableSource
apply
(
Observable
upstream
)
{
...
...
@@ -168,4 +131,31 @@ public class Api {
}
};
/**
* 对网络接口返回的Response进行分割操作
*
* @param response
* @param <T>
* @return
*/
public
static
<
T
>
Observable
<
T
>
flatResponse
(
final
BaseResponse
<
T
>
response
)
{
return
Observable
.
create
(
e
->
{
if
(
response
.
isSuccess
())
{
if
(!
e
.
isDisposed
())
{
e
.
onNext
(
response
.
getData
());
}
}
else
{
if
(!
e
.
isDisposed
())
{
e
.
onError
(
new
ServerException
(
response
.
getSubCode
(),
response
.
getMsg
()));
}
return
;
}
if
(!
e
.
isDisposed
())
{
e
.
onComplete
();
}
});
}
}
app/src/main/java/com/dayu/bigfish/utils/managers/DownloadManager.java
View file @
b7e10718
...
...
@@ -13,6 +13,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
import
io.reactivex.disposables.CompositeDisposable
;
import
io.reactivex.disposables.Disposable
;
import
io.reactivex.schedulers.Schedulers
;
import
okhttp3.ResponseBody
;
import
okio.BufferedSink
;
import
okio.BufferedSource
;
import
okio.Okio
;
...
...
@@ -32,8 +33,8 @@ public class DownloadManager {
* @param cd 订阅关系集合,在数据传输完毕时解除订阅
*/
public
static
void
download
(
final
Context
context
,
final
String
url
,
final
String
apkPath
,
final
CompositeDisposable
cd
,
onDownloadListener
listener
)
{
Api
.
getDownload
Retrofit
().
download
(
url
)
.
map
(
responseBody
->
responseBody
.
source
()
)
Api
.
getDownload
Service
().
download
(
url
)
.
map
(
ResponseBody:
:
source
)
.
subscribeOn
(
Schedulers
.
io
())
.
observeOn
(
Schedulers
.
io
())
.
doOnNext
(
bufferedSource
->
{
...
...
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