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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章