8
8
import com .surcumference .fingerprint .R ;
9
9
import com .surcumference .fingerprint .bean .UpdateInfo ;
10
10
import com .surcumference .fingerprint .network .inf .UpdateResultListener ;
11
+ import com .surcumference .fingerprint .network .interceptor .UrlFallbackInterceptor ;
11
12
import com .surcumference .fingerprint .network .update .BaseUpdateChecker ;
12
13
import com .surcumference .fingerprint .network .update .github .bean .GithubAssetsInfo ;
13
14
import com .surcumference .fingerprint .network .update .github .bean .GithubLatestInfo ;
16
17
import com .surcumference .fingerprint .util .log .L ;
17
18
18
19
import java .io .IOException ;
20
+ import java .util .Arrays ;
21
+ import java .util .Collections ;
19
22
import java .util .Date ;
20
23
21
24
import okhttp3 .Call ;
22
25
import okhttp3 .Callback ;
23
26
import okhttp3 .OkHttpClient ;
27
+ import okhttp3 .Protocol ;
24
28
import okhttp3 .Request ;
25
29
import okhttp3 .Response ;
26
30
27
31
/**
28
32
* Created by Jason on 2017/9/9.
29
33
*/
30
-
31
34
public class GithubUpdateChecker extends BaseUpdateChecker {
32
35
33
- public static OkHttpClient sHttpClient = new OkHttpClient () ;
36
+ public final OkHttpClient mHttpClient ;
34
37
private final String mLocalVersion ;
35
- private final String mUpdateUrl ;
38
+ private final String [] mUpdateUrls ;
36
39
37
- public GithubUpdateChecker (String localVersion , String updateUrl , UpdateResultListener listener ) {
40
+ public GithubUpdateChecker (String localVersion , String [] updateUrls , UpdateResultListener listener ) {
38
41
super (listener );
42
+ if (updateUrls .length == 0 ) {
43
+ throw new IllegalArgumentException ("Error: expected update urls, got zero" );
44
+ }
39
45
this .mLocalVersion = localVersion ;
40
- this .mUpdateUrl = updateUrl ;
46
+ this .mUpdateUrls = updateUrls ;
47
+ OkHttpClient .Builder builder = new OkHttpClient .Builder ()
48
+ .retryOnConnectionFailure (true );
49
+ if (updateUrls .length > 1 ) {
50
+ String [] fallbackUrls = Arrays .copyOfRange (updateUrls , 1 , updateUrls .length );
51
+ builder .addInterceptor (new UrlFallbackInterceptor (fallbackUrls ));
52
+ }
53
+ this .mHttpClient = builder .build ();
41
54
}
42
55
43
56
@ Override
@@ -57,7 +70,7 @@ public void onResponse(Call call, Response response) throws IOException {
57
70
try {
58
71
GithubLatestInfo info = new Gson ().fromJson (replay , GithubLatestInfo .class );
59
72
if (!info .isDataComplete ()) {
60
- onNetErr (new IllegalArgumentException ("data not complete!" ));
73
+ onNetErr (new IllegalArgumentException ("data not complete!, data: " + replay ));
61
74
return ;
62
75
}
63
76
if (BuildConfig .DEBUG || StringUtils .isAppNewVersion (mLocalVersion , info .version )) {
@@ -81,9 +94,9 @@ public void onResponse(Call call, Response response) throws IOException {
81
94
};
82
95
83
96
Request request = new Request .Builder ()
84
- .url (this .mUpdateUrl )
97
+ .url (this .mUpdateUrls [ 0 ] )
85
98
.build ();
86
- sHttpClient .newCall (request ).enqueue (callback );
99
+ mHttpClient .newCall (request ).enqueue (callback );
87
100
}
88
101
89
102
private String appendUpdateExtInfo (String content , Date date , String pageUrl ) {
0 commit comments