学习网络请求返回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);
                    }
                }
原文地址:https://www.cnblogs.com/caoxinyu/p/10568674.html