Skip to content

Commit 6f5c029

Browse files
committed
FastObserver增加 FastNullException错误判断用于处理部分特殊情况
1 parent eb4b9a8 commit 6f5c029

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

app/src/main/java/com/aries/library/fast/demo/base/BaseEntity.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.aries.library.fast.demo.base;
22

33
/**
4-
* Created: AriesHoo on 2017/6/29 16:39
5-
* Function:
6-
* Desc:
4+
* @Author: AriesHoo on 2019/7/11 22:00
5+
* @E-Mail: AriesHoo@126.com
6+
* @Function:
7+
* @Description:
78
*/
89
public class BaseEntity<T> {
10+
11+
public boolean success;
912
public int code;
1013
public String msg;
1114
public T data;

app/src/main/java/com/aries/library/fast/demo/retrofit/repository/BaseRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.accounts.NetworkErrorException;
44

55
import com.aries.library.fast.demo.base.BaseEntity;
6+
import com.aries.library.fast.retrofit.FastNullException;
67
import com.aries.library.fast.retrofit.FastRetryWhen;
78
import com.aries.library.fast.retrofit.FastTransformer;
89

@@ -30,7 +31,12 @@ protected <T> Observable<T> transform(Observable<BaseEntity<T>> observable) {
3031
if (result == null) {
3132
return Observable.error(new NetworkErrorException());
3233
} else {
33-
return Observable.just(result.data);
34+
if (result.success) {
35+
return result.data != null ? Observable.just(result.data)
36+
: Observable.error(new FastNullException());
37+
} else {
38+
return Observable.error(new NetworkErrorException());
39+
}
3440
}
3541
}));
3642
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.aries.library.fast.retrofit;
2+
3+
/**
4+
* @Author: AriesHoo on 2019/7/11 21:55
5+
* @E-Mail: AriesHoo@126.com
6+
* @Function: 特定空对象Exception 用于解决接口某些情况下数据null无法回调{@link FastObserver#_onNext(Object)}的情况
7+
* @Description:
8+
*/
9+
public class FastNullException extends Exception {
10+
11+
public FastNullException(String message) {
12+
super(message);
13+
}
14+
15+
public FastNullException(String message, Throwable cause) {
16+
super(message, cause);
17+
}
18+
19+
public FastNullException(Throwable cause) {
20+
super(cause);
21+
}
22+
}

library/src/main/java/com/aries/library/fast/retrofit/FastObserver.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public void onComplete() {
3434

3535
@Override
3636
public void onError(Throwable e) {
37+
if (e instanceof FastNullException) {
38+
_onNext(null);
39+
return;
40+
}
3741
if (FastManager.getInstance().getHttpRequestControl() != null) {
3842
FastManager.getInstance().getHttpRequestControl().httpRequestError(mHttpRequestControl, e);
3943
}

0 commit comments

Comments
 (0)