Skip to content

Commit 2e5a712

Browse files
committed
于BaseNetCallService以及BaseNetCallFragment中也添加支持不同的网络响应类型的通用代码
1 parent 2d0874c commit 2e5a712

3 files changed

Lines changed: 192 additions & 4 deletions

File tree

src/main/java/common/base/fragments/BaseNetCallFragment.java

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package common.base.fragments;
22

3+
import common.base.activitys.BaseNetCallActivity;
34
import common.base.netAbout.INetEvent;
45
import common.base.netAbout.NetDataAndErrorListener;
56
import common.base.netAbout.NetRequestLifeMarker;
@@ -81,15 +82,106 @@ public void onErrorBeforeRequest(int curRequestDataType, int errorType) {
8182

8283
protected void initNetDataListener() {
8384
if (netDataAndErrorListener == null) {
84-
netDataAndErrorListener = new NetDataAndErrorListener<T>(this);
85+
netDataAndErrorListener = createANetListener();
8586
}
8687
}
8788

8889
/**
8990
* 如果一个Fragment界面中可能需求多个网络请求,则可调用该方法生成对应的网络响应监听者
9091
* @return
9192
*/
92-
protected NetDataAndErrorListener createANetListener() {
93-
return new NetDataAndErrorListener<T>(this);
93+
protected NetDataAndErrorListener<T> createANetListener() {
94+
return new NetDataAndErrorListener<>(this);
95+
}
96+
97+
//-------- added by fee 2016-12-14 ---------------------------
98+
/**
99+
*由于本基类的网络请求响应类型T一旦指定后,就只能响应一种类型的,而一些使用场景可能存在不同网络请求接口
100+
响应的数据类型可能和本基类所指定的T类型不相同(比如:如果本基类T被指定成JSONObject类型的,则要求所请求的网络接口都是满足是JsonObject类型数据,
101+
这时,如果有一个网络请求响应的不是JsonObject类型的,如github api接口就有响应的数据为JSONArray类型的,
102+
则这时子类就不能使用基类的netDataAndErrorListener
103+
以及createANetListener()了),所以为了通用各种响应类型,增加以下代码
104+
使用方法:如果本基类的网络请求响应类型被指定为用户自定义的对象如{@linkplain common.base.netAbout.BaseServerResult},而如果有一个网络请求
105+
假设为 BaseApi.getRespoOfAUser(String curGithubUser,NetDataAndErrorListener<List<Respo>>callback);此时直接调用该API并且传入本基类的
106+
netDataAndErrorListener或者createANetListener()都不匹配了,则需要这样使用
107+
NetDataAndErrorListener<List<Respo> listenner = createETypeListener();
108+
再调用BaseApi.getRespoOfAUser("feer921",listenner);
109+
* @param <E>
110+
* @return 满足此次网络请求响应的数据类型的监听者
111+
*/
112+
protected <E> NetDataAndErrorListener<E> createETypeListener() {
113+
return new NetDataAndErrorListener<>(new ETypeNetEvent<E>());
114+
}
115+
/**
116+
* 子类如果有不同于本基类BaseNetCallActivity所指定的网络响应类型T
117+
* 的网络请求响应数据类型,那么可以直接new{@linkplain NetDataAndErrorListener}时传入本类New ETypeNetEvent(E)
118+
* @param <E>
119+
*/
120+
public final class ETypeNetEvent<E> implements INetEvent<E>{
121+
private final static String LOG_TAG = "ETypeNetEvent";
122+
/**
123+
* 网络请求失败
124+
*
125+
* @param requestDataType 当前请求类型
126+
* @param errorInfo 错误信息
127+
*/
128+
@Override
129+
public void onErrorResponse(int requestDataType, String errorInfo) {
130+
if (LIFE_DEBUG) {
131+
e(TAG, LOG_TAG + "--> onErrorResponse() requestDataType = " + requestDataType + " errorInfo = " + errorInfo);
132+
}
133+
if (requestDataType <= 0) {
134+
//是否需要加此判断呢??
135+
return;
136+
}
137+
//如果用户主动取消了当前网络请求如Loading dialog被取消了(实际上该请求已到达服务端,因而会响应回调)
138+
//则不让各子类处理已被用户取消了的请求
139+
if (curRequestCanceled(requestDataType)) {
140+
return;
141+
}
142+
addRequestStateMark(requestDataType, NetRequestLifeMarker.REQUEST_STATE_FINISHED);
143+
dealWithErrorResponse(requestDataType,errorInfo);
144+
}
145+
146+
/**
147+
* 网络请求的响应
148+
*
149+
* @param requestDataType 当前网络请求数据类型
150+
* @param result 响应实体
151+
*/
152+
@Override
153+
public void onResponse(int requestDataType, E result) {
154+
if (LIFE_DEBUG) {
155+
i(TAG, LOG_TAG + "--> onResponse() requestDataType = " + requestDataType + " result = " + result);
156+
}
157+
if (requestDataType <= 0) {
158+
//是否要加此判断呢??本框架使用者,都应该用一个整数值区分是哪个网络请求接口吧
159+
return;
160+
}
161+
if (curRequestCanceled(requestDataType)) {
162+
return;
163+
}
164+
addRequestStateMark(requestDataType, NetRequestLifeMarker.REQUEST_STATE_FINISHED);
165+
dealWithETypeResponse(requestDataType,result);
166+
}
167+
168+
/**
169+
* 错误回调,在还没有开始请求之前,比如:一些参数错误
170+
*
171+
* @param curRequestDataType 当前网络请求类型
172+
* @param errorType 错误类型
173+
*/
174+
@Override
175+
public void onErrorBeforeRequest(int curRequestDataType, int errorType) {
176+
BaseNetCallFragment.this.onErrorBeforeRequest(curRequestDataType, errorType);
177+
}
178+
}
179+
180+
/***
181+
* 用来处理非本基类被指定的网络请求响应数据类型T类型,而是其他网络响应类型的结果
182+
* @param requestDataType 当前网络请求类型
183+
* @param responseResut 网络请求响应结果 这里为Object对象类型来通用,子类如果处理此回调时,自己强转成预期对象类型
184+
*/
185+
protected void dealWithETypeResponse(int requestDataType, Object responseResut) {
94186
}
95187
}

src/main/java/common/base/services/BaseNetCallService.java

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package common.base.services;
22

3+
import common.base.activitys.BaseNetCallActivity;
34
import common.base.netAbout.INetEvent;
45
import common.base.netAbout.NetDataAndErrorListener;
56
import common.base.netAbout.NetRequestLifeMarker;
@@ -23,7 +24,7 @@ public void onCreate() {
2324

2425
protected void initANetDataAndErrorListener() {
2526
if (mNetDataAndErrorListener == null) {
26-
mNetDataAndErrorListener = new NetDataAndErrorListener<>(this);
27+
mNetDataAndErrorListener = createANewNetListener();
2728
}
2829
}
2930
protected NetDataAndErrorListener<T> createANewNetListener() {
@@ -95,4 +96,95 @@ protected void dealWithResponse(int requestDataType, T result) {
9596
public void onErrorBeforeRequest(int curRequestDataType, int errorType) {
9697

9798
}
99+
100+
//-------- added by fee 2016-12-14 ---------------------------
101+
/**
102+
*由于本基类的网络请求响应类型T一旦指定后,就只能响应一种类型的,而一些使用场景可能存在不同网络请求接口
103+
响应的数据类型可能和本基类所指定的T类型不相同(比如:如果本基类T被指定成JSONObject类型的,则要求所请求的网络接口都是满足是JsonObject类型数据,
104+
这时,如果有一个网络请求响应的不是JsonObject类型的,如github api接口就有响应的数据为JSONArray类型的,
105+
则这时子类就不能使用基类的netDataAndErrorListener
106+
以及createANetListener()了),所以为了通用各种响应类型,增加以下代码
107+
使用方法:如果本基类的网络请求响应类型被指定为用户自定义的对象如{@linkplain common.base.netAbout.BaseServerResult},而如果有一个网络请求
108+
假设为 BaseApi.getRespoOfAUser(String curGithubUser,NetDataAndErrorListener<List<Respo>>callback);此时直接调用该API并且传入本基类的
109+
netDataAndErrorListener或者createANetListener()都不匹配了,则需要这样使用
110+
NetDataAndErrorListener<List<Respo> listenner = createETypeListener();
111+
再调用BaseApi.getRespoOfAUser("feer921",listenner);
112+
* @param <E>
113+
* @return 满足此次网络请求响应的数据类型的监听者
114+
*/
115+
protected <E> NetDataAndErrorListener<E> createETypeListener() {
116+
return new NetDataAndErrorListener<>(new ETypeNetEvent<E>());
117+
}
118+
/**
119+
* 子类如果有不同于本基类BaseNetCallActivity所指定的网络响应类型T
120+
* 的网络请求响应数据类型,那么可以直接new{@linkplain NetDataAndErrorListener}时传入本类New ETypeNetEvent(E)
121+
* @param <E>
122+
*/
123+
public final class ETypeNetEvent<E> implements INetEvent<E>{
124+
private final static String LOG_TAG = "ETypeNetEvent";
125+
/**
126+
* 网络请求失败
127+
*
128+
* @param requestDataType 当前请求类型
129+
* @param errorInfo 错误信息
130+
*/
131+
@Override
132+
public void onErrorResponse(int requestDataType, String errorInfo) {
133+
if (LIFE_CIRCLE_DEBUG) {
134+
e(TAG, LOG_TAG + "--> onErrorResponse() requestDataType = " + requestDataType + " errorInfo = " + errorInfo);
135+
}
136+
if (requestDataType <= 0) {
137+
//是否需要加此判断呢??
138+
return;
139+
}
140+
//如果用户主动取消了当前网络请求如Loading dialog被取消了(实际上该请求已到达服务端,因而会响应回调)
141+
//则不让各子类处理已被用户取消了的请求
142+
if (curRequestCanceled(requestDataType)) {
143+
return;
144+
}
145+
addRequestStateMark(requestDataType, NetRequestLifeMarker.REQUEST_STATE_FINISHED);
146+
dealWithErrorResponse(requestDataType,errorInfo);
147+
}
148+
149+
/**
150+
* 网络请求的响应
151+
*
152+
* @param requestDataType 当前网络请求数据类型
153+
* @param result 响应实体
154+
*/
155+
@Override
156+
public void onResponse(int requestDataType, E result) {
157+
if (LIFE_CIRCLE_DEBUG) {
158+
i(TAG, LOG_TAG + "--> onResponse() requestDataType = " + requestDataType + " result = " + result);
159+
}
160+
if (requestDataType <= 0) {
161+
//是否要加此判断呢??本框架使用者,都应该用一个整数值区分是哪个网络请求接口吧
162+
return;
163+
}
164+
if (curRequestCanceled(requestDataType)) {
165+
return;
166+
}
167+
addRequestStateMark(requestDataType, NetRequestLifeMarker.REQUEST_STATE_FINISHED);
168+
dealWithETypeResponse(requestDataType,result);
169+
}
170+
171+
/**
172+
* 错误回调,在还没有开始请求之前,比如:一些参数错误
173+
*
174+
* @param curRequestDataType 当前网络请求类型
175+
* @param errorType 错误类型
176+
*/
177+
@Override
178+
public void onErrorBeforeRequest(int curRequestDataType, int errorType) {
179+
BaseNetCallService.this.onErrorBeforeRequest(curRequestDataType, errorType);
180+
}
181+
}
182+
183+
/***
184+
* 用来处理非本基类被指定的网络请求响应数据类型T类型,而是其他网络响应类型的结果
185+
* @param requestDataType 当前网络请求类型
186+
* @param responseResut 网络请求响应结果 这里为Object对象类型来通用,子类如果处理此回调时,自己强转成预期对象类型
187+
*/
188+
protected void dealWithETypeResponse(int requestDataType, Object responseResut) {
189+
}
98190
}

src/main/java/common/base/utils/CommonLog.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
public final class CommonLog {
1313
// public final static boolean ISDEBUG = BuildConfig.DEBUG;
1414
public static boolean ISDEBUG = BuildConfig.DEBUG;
15+
16+
public static void logEnable(boolean toEnable) {
17+
ISDEBUG = toEnable;
18+
}
1519
public static void w(String tag, String content) {
1620
if (ISDEBUG) {
1721
android.util.Log.w(tag, content);

0 commit comments

Comments
 (0)