Skip to content

Commit 057c67d

Browse files
author
liuyongkui
committed
Merge branch '1.x' of https://github.com/Tamicer/Novate into 1.x
Conflicts: novate/build.gradle novate/src/main/java/com/tamic/novate/BaseApiService.java novate/src/main/java/com/tamic/novate/Novate.java
2 parents 006c49b + 5df12e7 commit 057c67d

File tree

6 files changed

+44
-25
lines changed

6 files changed

+44
-25
lines changed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
buildscript {
44
repositories {
55
jcenter()
6-
//maven { url "http://maven.ipo.com/nexus/content/groups/public/" }
76
maven { url "https://jitpack.io" }
87

98
}
@@ -21,7 +20,6 @@ buildscript {
2120
allprojects {
2221
repositories {
2322
maven { url "https://jitpack.io" }
24-
//maven { url "http://maven.ipo.com/nexus/content/groups/public/" }
2523
jcenter()
2624
}
2725
}

exemple/src/main/java/com/tamic/excemple/ExampleActivity.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.HashMap;
3838
import java.util.List;
3939
import java.util.Map;
40+
import java.util.concurrent.TimeUnit;
4041

4142
import okhttp3.Call;
4243
import okhttp3.Callback;
@@ -98,7 +99,6 @@ protected void onCreate(Bundle savedInstanceState) {
9899
.readTimeout(30)
99100
.baseUrl(baseUrl)
100101
.addHeader(headers)
101-
.addCache(true)
102102
.addLog(true)
103103
.build();
104104

@@ -345,7 +345,15 @@ public void onNext(ResponseBody responseBody) {
345345
*/
346346
private void performGet() {
347347

348-
348+
novate = new Novate.Builder(this)
349+
.connectTimeout(30)
350+
.writeTimeout(15)
351+
.readTimeout(30)
352+
.baseUrl(baseUrl)
353+
.addHeader(headers)
354+
.addCache(false)
355+
.addLog(true)
356+
.build();
349357
/**
350358
* 如果不需要数据解析后返回 则调用novate.Get()
351359
* 参考 performPost()中的方式
@@ -376,7 +384,7 @@ public void onSuccee(NovateResponse<ResultModel> response) {
376384

377385
@Override
378386
public void onsuccess(int code, String msg, ResultModel response, String originalResponse) {
379-
Toast.makeText(ExampleActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
387+
Toast.makeText(ExampleActivity.this, response != null ? response.toString(): "无数据", Toast.LENGTH_SHORT).show();
380388
}
381389

382390
});

exemple/src/main/java/com/tamic/excemple/RequstActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private void performFile() {
434434
String downUrl = "http://img06.tooopen.com/images/20161022/tooopen_sy_182719487645.jpg";
435435
String path = FileUtil.getBasePath(this);
436436

437-
novate.rxGet(downUrl, new RxFileCallBack(path, "my.jpg") {
437+
novate.rxGet(downUrl, parameters, new RxFileCallBack(path, "my.jpg") {
438438

439439

440440
@Override

novate/src/main/java/com/tamic/novate/BaseApiService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ <T> Observable<ResponseBody> executePost(
4040

4141
@POST()
4242
Observable<ResponseBody> executePostBody(
43-
@Url() String url,
43+
@Url String url,
4444
@Body Object object);
4545

4646
@GET()

novate/src/main/java/com/tamic/novate/Novate.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public final class Novate {
127127
private static final int DEFAULT_MAXIDLE_CONNECTIONS = 5;
128128
private static final long DEFAULT_KEEP_ALIVEDURATION = 8;
129129
private static final long DEFAULT_CACHEMAXSIZE = 10 * 1024 * 1024;
130+
private static int DEFAULT_MAX_STALE = 60 * 60 * 24 * 3;
130131
public static final String TAG = "Novate";
131132

132133
/**
@@ -1574,7 +1575,8 @@ public static final class Builder {
15741575
private int readTimeout = DEFAULT_TIMEOUT;
15751576
private int default_maxidle_connections = DEFAULT_MAXIDLE_CONNECTIONS;
15761577
private long default_keep_aliveduration = DEFAULT_MAXIDLE_CONNECTIONS;
1577-
private long caheMaxSize = DEFAULT_CACHEMAXSIZE;
1578+
private long cacheMaxSize = DEFAULT_CACHEMAXSIZE;
1579+
private int cacheTimeout = DEFAULT_MAX_STALE;
15781580
private okhttp3.Call.Factory callFactory;
15791581
private String baseUrl;
15801582
private Boolean isLog = false;
@@ -1710,6 +1712,18 @@ public Builder addCookie(boolean isCookie) {
17101712
return this;
17111713
}
17121714

1715+
1716+
/**
1717+
* set Cache MaxSize
1718+
* @param size MaxSize unit kb
1719+
* def 10 * 1024 * 1024
1720+
* @return
1721+
*/
1722+
public Builder addCacheMaxSize(int size) {
1723+
this.cacheMaxSize = size;
1724+
return this;
1725+
}
1726+
17131727
/**
17141728
* open default Cache
17151729
*
@@ -1734,8 +1748,8 @@ public Builder proxy(Proxy proxy) {
17341748
* TimeUnit {@link TimeUnit}
17351749
*/
17361750
public Builder writeTimeout(int timeout, TimeUnit unit) {
1737-
this.writeTimeout = Utils.checkDuration("timeout", timeout, unit);
1738-
if (timeout != -1) {
1751+
this.writeTimeout = timeout;
1752+
if (timeout >= 0) {
17391753
okhttpBuilder.writeTimeout(timeout, unit);
17401754
}
17411755
return this;
@@ -1748,8 +1762,8 @@ public Builder writeTimeout(int timeout, TimeUnit unit) {
17481762
* TimeUnit {@link TimeUnit}
17491763
*/
17501764
public Builder readTimeout(int timeout, TimeUnit unit) {
1751-
this.readTimeout = Utils.checkDuration("timeout", timeout, unit);
1752-
if (timeout != -1) {
1765+
if (readTimeout > 0) {
1766+
this.readTimeout = timeout;
17531767
okhttpBuilder.readTimeout(readTimeout, unit);
17541768
}
17551769
return this;
@@ -1778,8 +1792,8 @@ public Builder connectionPool(ConnectionPool connectionPool) {
17781792
*/
17791793
public Builder connectTimeout(int timeout, TimeUnit unit) {
17801794
this.readTimeout = Utils.checkDuration("timeout", timeout, unit);
1781-
;
1782-
if (timeout != -1) {
1795+
if (timeout >= 0) {
1796+
this.readTimeout = timeout;
17831797
okhttpBuilder.connectTimeout(readTimeout, unit);
17841798
}
17851799
return this;
@@ -1919,23 +1933,23 @@ public Builder addNetworkInterceptor(Interceptor interceptor) {
19191933
* @return Builder
19201934
*/
19211935
public Builder addCache(Cache cache) {
1922-
int maxStale = 60 * 60 * 24 * 3;
1923-
return addCache(cache, maxStale);
1936+
return addCache(cache, cacheTimeout);
19241937
}
19251938

19261939
/**
19271940
* @param cache
1928-
* @param cacheTime ms
1941+
* @param cacheTimeOut ms
19291942
* @return
19301943
*/
1931-
public Builder addCache(Cache cache, final int cacheTime) {
1932-
addCache(cache, String.format("max-age=%d", cacheTime));
1944+
public Builder addCache(Cache cache, final int cacheTimeOut) {
1945+
addCache(cache, String.format("max-age=%d", cacheTimeOut));
19331946
return this;
19341947
}
19351948

19361949
/**
19371950
* @param cache
19381951
* @param cacheControlValue Cache-Control
1952+
* def max-age=
19391953
* @return
19401954
*/
19411955
private Builder addCache(Cache cache, final String cacheControlValue) {
@@ -1984,7 +1998,6 @@ public Novate build() {
19841998
if (converterFactory == null) {
19851999
converterFactory = GsonConverterFactory.create();
19862000
}
1987-
;
19882001

19892002
retrofitBuilder.addConverterFactory(converterFactory);
19902003
/**
@@ -2026,15 +2039,15 @@ public Novate build() {
20262039
if (isCache) {
20272040
try {
20282041
if (cache == null) {
2029-
cache = new Cache(httpCacheDirectory, caheMaxSize);
2042+
cache = new Cache(httpCacheDirectory, cacheMaxSize);
20302043
}
20312044
addCache(cache);
20322045

20332046
} catch (Exception e) {
20342047
Log.e("OKHttp", "Could not create http cache", e);
20352048
}
20362049
if (cache == null) {
2037-
cache = new Cache(httpCacheDirectory, caheMaxSize);
2050+
cache = new Cache(httpCacheDirectory, cacheMaxSize);
20382051
}
20392052
}
20402053

novate/src/main/java/com/tamic/novate/NovateSubscriber.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void onNext(ResponseBody responseBody) {
119119
baseResponse.setMessage(msg);
120120
dataStr = jsonObject.opt("data").toString();
121121
if (dataStr.isEmpty()) {
122-
dataStr = jsonObject.opt("result").toString();
122+
dataStr = jsonObject.optString("result");
123123
}
124124

125125
if (dataStr.isEmpty()) {
@@ -137,7 +137,7 @@ public void onNext(ResponseBody responseBody) {
137137
throw new FormatException();
138138
}
139139

140-
} else if (dataStr.charAt(0) == '[') {
140+
} else if (!dataStr.isEmpty() && dataStr.charAt(0) == '[') {
141141
LogWraper.e(TAG, "data为数对象无法转换: --- " + finalNeedType);
142142
//dataStr = jsonObject.optJSONArray("data").toString();
143143
//dataResponse = (T) new Gson().fromJson(dataStr, finalNeedType);
@@ -159,7 +159,7 @@ public void onNext(ResponseBody responseBody) {
159159
baseResponse.setData(dataResponse);
160160
}
161161

162-
if (baseResponse.isOk(context) && dataResponse == null) {
162+
if (dataResponse != null && baseResponse.isOk(context)) {
163163

164164
LogWraper.d(TAG, "Response data 数据获取失败!");
165165
callBack.onsuccess(0, "", null, jsStr);

0 commit comments

Comments
 (0)