Android---构建一个自己的网络框架以及源码
第一步构建请求
构建请求的抽象类:
import java.io.Serializable;
public abstract class RequestBean implements Serializable {
public abstract String getRequestKey();
public abstract String getRequestStr();
public abstract TextMessageParser getMessageParser();
}
在请求抽象类中,getRequestKey()为获取请求的标识和路径,getRequestStr()为获取请求的参数,getMessageParser()为获取请求的解析器。
其中,TextMessageParser为解析器抽象类:
public abstract class TextMessageParser {
public abstract ResponseBean parser(String resp);
}
这里贴个请求的实体类demo:
public class ListenAppListReq extends RequestBean {
private String token = "";
private ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public ArrayList<HashMap<String, Object>> getListItems() {
return listItems;
}
public void setListItems(ArrayList<HashMap<String, Object>> listItems) {
this.listItems = listItems;
}
@Override
public String getRequestKey() {
return RequestKey.APP_KEY;
}
@Override
public String getRequestStr() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("action", RequestKey.APP_KEY);
map.put("token", token);
return JSONBuildHelper.buildMessage(map, listItems);
}
@Override
public TextMessageParser getMessageParser() {
return new ListenAppListParser();
}
}
RequestKey类为请求路径和请求标识静态类:
public interface RequestKey {
/** 登录 */
final String LOGIN_KEY = "0";
final String APP_KEY = "1";
final String APP_START_KEY = "2";
final String HEART_KEY = "3";
final String UP_DATA = "/update";
}
JSONBuildHelper类为请求参数组建类:
public class JSONBuildHelper {
public static String buildMessage(Map<String, String> map) {
String object = "";
Set<Map.Entry<String, String>> entrySet = map.entrySet();
int i = 0;
for (Map.Entry<String, String> entry : entrySet) {
if (i == 0) {
object = entry.getKey() + "=" + entry.getValue();
} else {
object = object + "&" + entry.getKey() + "=" + entry.getValue();
}
i = i + 1;
}
return object;
}
public static String buildMessage(HashMap<String, String> map) {
JSONObject object =new JSONObject();
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
try {
object.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
return object.toString();
}
public static String buildMessage(HashMap<String, String> map, ArrayList<HashMap<String, Object>> listItems) {
JSONObject object =new JSONObject();
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
try {
object.put(entry.getKey(), entry.getValue());
} catch (JSONException e) {
// e.printStackTrace();
}
}
if (listItems != null && listItems.size() > 0) {
JSONArray dataArray = new JSONArray();
for (int i=0;i<listItems.size();i++) {
dataArray.put(listItems.get(i).get("ItemTitle"));
}
try {
object.put("data", dataArray);
} catch (JSONException e) {
// e.printStackTrace();
}
}
return object.toString();
}
}
请求数据回调抽象类:
public interface ProcessListener {
public boolean onDone(ResponseBean responseBean);
}