zoukankan      html  css  js  c++  java
  • 学习网络请求返回json对应的model

    原来泛型可以这样用:

    网络返回基类,返回一个code,msg,body,其中body不确定,所以,我们把它写成泛型

    import org.json.JSONObject;
    
    /**
     * 网络请求的基类
     * Created by  on 16/7/14.
     */
    public class NetData<T> {
        public static final int STATUS_OK = 0;
        public int code = -1;
        public String msg;
        public T body;
    
        /**
         * code码是否正确
         * @return
         */
        public boolean isCodeOk(){
            return code == STATUS_OK;
        }
    
    
        public JSONObject getJSONBody(JSONObject data){
            if(data == null){
                return null;
            }
    
            return data.optJSONObject("body");
        }
    
        public JSONObject getJSONBody(String data){
            JSONObject jsonObj = convertStrToJSON(data);
            return getJSONBody(jsonObj);
        }
    
        public void parseHead(String json){
            JSONObject jsonObject = convertStrToJSON(json);
            if(jsonObject != null){
                parseHead(jsonObject);
            }
        }
    
        public void parseHead(JSONObject obj){
            try {
                if(obj == null){
                    return;
                }
                code = obj.optInt("code");
                msg = obj.optString("msg");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        public JSONObject convertStrToJSON(String json){
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
            }catch (Exception e){
                e.printStackTrace();
            }
    
            return jsonObject;
        }
    }

    具体类:

    public class PlugUpdateInfo {
        public boolean mNeedUpdate;
        public String mVersion;
        public String mDownloadUrl;
        public boolean mForcedUpdate;
    
        /**
         * 解析数据
         * @param jsonStr
         * @return true,解析成功;false解析失败
         */
        public boolean parse(String jsonStr){
            JSONObject json = convertStrToJSON(jsonStr);
            return parse(json);
        }
    
        public boolean parse(JSONObject json){
            if(json == null){
                return false;
            }
    
            boolean needReflesh = json.optBoolean(Constants.NEED_UPDATE);
            String serverVersion = json.optString(Constants.VERSION);
            String mDownLoadUrl = json.optString(Constants.DOWNLOAD_URL);
            int  mforceUpdate = json.optInt(Constants.FORCED_UPDATAE); //强制更新,0:否,1:是
    
            this.mNeedUpdate = needReflesh;
            this.mVersion = serverVersion;
            this.mDownloadUrl = mDownLoadUrl;
            this.mForcedUpdate = mforceUpdate == 1?true:false;
    
            return true;
        }
    
        public JSONObject convertStrToJSON(String json){
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
            }catch (JSONException e){
                e.printStackTrace();
            }
    
            return jsonObject;
        }
    }

    组合:

    NetData<PlugUpdateInfo> baseData = new NetData<PlugUpdateInfo>();
                    baseData.parseHead(result);
    
                    if(baseData.isCodeOk()){
                        JSONObject jsonBody = baseData.getJSONBody(result);
    
                        PlugUpdateInfo data = new PlugUpdateInfo();
                        boolean success = data.parse(jsonBody);
                        if(success){
                            if(listener != null){
                                listener.onRequestDataSuccess(data);
                            }
                        }else{
                            if(listener != null){
                                listener.onRequestDataFailed(-1, "");
                            }
                        }
                    }else{
                        if(listener != null){
                            listener.onRequestDataFailed(baseData.code, baseData.msg);
                        }
                    }
  • 相关阅读:
    【Uvalive4960】 Sensor network (苗条树,进化版)
    【UVA 1151】 Buy or Build (有某些特别的东东的最小生成树)
    【UVA 1395】 Slim Span (苗条树)
    【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
    【UVA 10369】 Arctic Network (最小生成树)
    【UVA 10816】 Travel in Desert (最小瓶颈树+最短路)
    【UVA 11183】 Teen Girl Squad (定根MDST)
    【UVA 11865】 Stream My Contest (二分+MDST最小树形图)
    【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)
    【LA 5713 】 Qin Shi Huang's National Road System (MST)
  • 原文地址:https://www.cnblogs.com/caoxinyu/p/6647782.html
Copyright © 2011-2022 走看看