Skip to content

Commit e3a9f8d

Browse files
committed
1、BaseNetCallActivity基类除该基类的网络响应类型T被指定了类型外,再增加可以生成不同网络响应数据类型的代码,使BaseNetCallActivity基类更加通用强大
1 parent c3c4d75 commit e3a9f8d

4 files changed

Lines changed: 261 additions & 5 deletions

File tree

src/main/java/common/base/activitys/BaseNetCallActivity.java

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

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import java.io.IOException;
6+
import java.util.List;
7+
import common.base.utils.CommonLog;
8+
import common.base.utils.JsonUtil;
9+
import common.base.utils.Util;
10+
311
/**
412
* User: fee(1176610771@qq.com)
513
* Date: 2016-05-17
614
* Time: 17:31
7-
* DESC: 服务端响应结果
15+
* DESC: 服务端响应结果转化成本地Java实体示例
16+
* 对应服务端的响应数据类型为Json,并且格式如下
17+
{
18+
"statusCode": "1",
19+
"msg": "请求成功",
20+
"data": {
21+
}
22+
}
823
*/
924
public class BaseServerResult {
25+
private static final String TAG = "BaseServerResult";
26+
public String statusCode;
27+
public String msg;
28+
/**
29+
* 这里是为了通用data字段对应的数据类型,有可能data对应就一个字符串 eg.:data:"",也有可能data对应的是
30+
* 一个Json对象 eg.: data:{}
31+
*/
32+
public JsonNode data;
1033
/**
11-
* 错误类型:没有网络
34+
* 这里是为了再把JsonNode的data再转化成字符串,方便使用Json序列化工具再转化成对应的Java实体类
1235
*/
13-
public static final int ERROR_CODE_NO_NET = 10;
14-
public boolean isOk = false;
36+
@JsonIgnore
37+
private String dataStr = "";
38+
39+
public boolean isResponseOk() {
40+
getDataStr();
41+
return "1".equals(statusCode);
42+
}
1543

44+
public String getDataStr() {
45+
if (!Util.isEmpty(dataStr)) {
46+
return dataStr;
47+
}
48+
if (data != null) {
49+
dataStr = data.toString();
50+
}
51+
return dataStr;
52+
}
53+
54+
public <T> T convertData2Bean(Class<T> beanClass) {
55+
try {
56+
return JsonUtil.jsonStr2Object(getDataStr(), beanClass);
57+
} catch (IOException e) {
58+
CommonLog.e("ServerResult","--> convertData2Bean() occur : " + e);
59+
}
60+
return null;
61+
}
62+
63+
/**
64+
* 如果服务端响应的data中为数组类型数据需要转换成集合
65+
* @param elementClass
66+
* @param <T>
67+
* @return
68+
*/
69+
public <T> List<T> convertData2List(Class<T> elementClass) {
70+
try {
71+
return JsonUtil.jsonArrayStr2ListObject(getDataStr(), elementClass);
72+
} catch (Exception e) {
73+
CommonLog.e(TAG, "--> convertData2List() e: " + e);
74+
}
75+
return null;
76+
}
1677
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package common.base.netAbout;
2+
3+
import android.util.Log;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import okhttp3.ResponseBody;
10+
import retrofit2.Call;
11+
import retrofit2.Response;
12+
13+
/**
14+
* User: fee(1176610771@qq.com)
15+
* Date: 2016-12-13
16+
* Time: 18:06
17+
* DESC: 使用Retrofit进行下载的网络回调,由于使用Retrofit进行下载时,网络请求响应的类型需要ResponseBody类型
18+
* 所以单独提供一个这样的响应监听对象
19+
* 注:下载大文件时请绕过,目前没有下载进度回调
20+
*/
21+
public class DownLoadCallbackListener extends NetDataAndErrorListener<ResponseBody> {
22+
private File toSave2LocalFile;
23+
public DownLoadCallbackListener(File toSaveContentFile, INetEvent<ResponseBody> netEvent) {
24+
super(netEvent);
25+
this.toSave2LocalFile = toSaveContentFile;
26+
}
27+
28+
@Override
29+
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
30+
boolean dealWithOk = false;
31+
if (response.isSuccessful()) {
32+
boolean writtenToDisk = writeResponseBodyToDisk(response.body());
33+
dealWithOk = writtenToDisk;
34+
if (!writtenToDisk) {
35+
if (toSave2LocalFile != null) {
36+
toSave2LocalFile.delete();
37+
}
38+
}
39+
}
40+
if (netEvent != null) {
41+
netEvent.onResponse(requestType, dealWithOk ? response.body() : null);
42+
}
43+
}
44+
45+
private boolean writeResponseBodyToDisk(ResponseBody body) {
46+
if (toSave2LocalFile != null) {
47+
InputStream inputStream = null;
48+
OutputStream outputStream = null;
49+
try {
50+
byte[] fileReader = new byte[4096];
51+
long fileSize = body.contentLength();
52+
long fileSizeDownloaded = 0;
53+
inputStream = body.byteStream();
54+
outputStream = new FileOutputStream(toSave2LocalFile);
55+
while (true) {
56+
int read = inputStream.read(fileReader);
57+
58+
if (read == -1) {
59+
break;
60+
}
61+
outputStream.write(fileReader, 0, read);
62+
fileSizeDownloaded += read;
63+
Log.d("info", "file download: " + fileSizeDownloaded + " of " + fileSize);
64+
}
65+
outputStream.flush();
66+
return true;
67+
} catch (IOException e) {
68+
return false;
69+
}
70+
finally {
71+
if (inputStream != null) {
72+
try {
73+
inputStream.close();
74+
} catch (IOException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
79+
if (outputStream != null) {
80+
try {
81+
outputStream.close();
82+
} catch (IOException e) {
83+
e.printStackTrace();
84+
}
85+
}
86+
}
87+
}
88+
return false;
89+
}
90+
}

src/main/java/common/base/netAbout/NetDataAndErrorListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
* DESC: 网络请求的回调监听者,其中的范型T代表当前的请求具体要返回何种类型的数据
1313
*/
1414
public class NetDataAndErrorListener<T> implements Callback<T>{
15+
protected final String TAG = getClass().getSimpleName();
1516
/**
1617
* 默认请求类型为-1,表示没有赋值请求类型
1718
*/
1819
public int requestType = -1;
19-
private INetEvent<T> netEvent;
20+
protected INetEvent<T> netEvent;
2021
public NetDataAndErrorListener(INetEvent<T> netEventCallback) {
2122
this.netEvent = netEventCallback;
2223
}

0 commit comments

Comments
 (0)