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);
                        }
                    }
  • 相关阅读:
    使用C#替换Word文档里的文字和图片
    《程序员的思维修炼—开发认知潜能的九堂课》—从新手到专家的历程
    从已有数据库表生成Insert语句的小工具
    我的2010
    Sqlite批量插入速度慢的解决方法小计
    分享一个winForm下的Chart控件
    分享一个任务提醒小工具
    SpringBoot+Vue+Echarts实现选择时间范围内数据加载显示柱状图
    Winform中选取指定文件夹并获取其下所有文件
    Vue中JS遍历后台JAVA返回的Map数据,构造对象数组数据格式
  • 原文地址:https://www.cnblogs.com/caoxinyu/p/6647782.html
Copyright © 2011-2022 走看看