package com.wasu.cs.protocol; import com.wasu.cs.model.Model; import com.wasu.module.datafetch.DataFetchListener; import com.wasu.module.datafetch.DataFetchModule; import org.json.JSONObject; public class BaseProtocol { private int code; private String message; private JSONObject json = null; public boolean from(JSONObject json) { if (null != json) { this.json = json; this.code = json.optInt("code", 0); this.message = json.optString("message", ""); return true; } return false; } public int getCode() { return code; } public String getMessage() { return message; } public boolean successed() { return code == 200 ? true : false; } public JSONObject getJSON() { return json; } public Model getData() { return null; } public interface FetchCallback { public void onResult(boolean successed, BaseProtocol protocol); } public static void fetch(String url, final FetchCallback callback,final BaseProtocol protocol) { DataFetchModule.getInstance().fetchJsonGet(url, new DataFetchListener.JsonListener() { @Override public void onJsonGet(int retcode, String extraMsg, JSONObject jsonObj) { if(callback == null) { try { throw new Exception(); } catch (Exception e) { e.printStackTrace(); } return; } if(retcode == 0) { protocol.from(jsonObj); callback.onResult(protocol.successed(), protocol); } else { callback.onResult(false, null); } } }); } }