Android---构建一个自己的网络框架(二)

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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值