Android項目開發所需的基礎框架
Android項目開發所需的基礎框架,包括網絡模塊、數據庫模塊、常用的工具類等。目前項目比較簡單,會在以后的工作和學習中不斷豐富、完善。
package com.daxiang.android.http; import java.util.List; import org.apache.http.NameValuePair; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils; import com.daxiang.android.bean.BaseRequest; import com.daxiang.android.http.HttpConstants.HttpMethod; import com.daxiang.android.http.core.HttpTool; import com.daxiang.android.utils.Logger; /** * * @author daxiang * * 2015-3-24 */ public class JsonUtil { private static final String TAG = JsonUtil.class.getSimpleName(); public static final String CACHE_JSON_SP = "cache_json_sp"; /** * 從server取數據,不做本地緩存; * * @param url * @param context * @param method * 請求方式 * @param postParameters * post請求參數 * @return */ public static String getJsonFromServer(String url, Context context, HttpMethod method, List<NameValuePair> postParameters) { return getJsonFromServer(url, false, context, method, postParameters); } /** * 從server取數據,可設置是否進行本地存儲; * * @param url * @param isCache * true,從服務器取數據并且進行本地存儲;false,沒有本地存儲; * @param context * @param method * 請求方式 * @param postParameters * post請求參數 * @return */ public static String getJsonFromServer(String url, boolean isCache, Context context, HttpMethod method, List<NameValuePair> postParameters) { Logger.i(TAG, "getJsonFromServer---" + url); String json = ""; if (method == HttpConstants.HttpMethod.GET) { json = HttpTool.httpGet(url); } else { json = HttpTool.httpPost(url, postParameters); } Logger.i(TAG, "getJsonFromServer---" + json); if (!TextUtils.isEmpty(json)) { if (isCache) { cacheJsonToFile(url, json, context); } return json; } return ""; } public static String getJsonFromServer(String url, Context context, HttpMethod method, BaseRequest bean) { return getJsonFromServer(url, false, context, method, bean); } public static String getJsonFromServer(String url, boolean isCache, Context context, HttpMethod method, BaseRequest bean) { Logger.i(TAG, "getJsonFromServer---" + url); // 按照約定當state == 200時說明數據正確 String json = ""; if (method == HttpConstants.HttpMethod.GET) { json = HttpTool.httpGet(url); } else if (method == HttpConstants.HttpMethod.POST) { json = HttpTool.httpPost(url, bean); } else if (method == HttpConstants.HttpMethod.DELETE) { json = HttpTool.httpDelete(url, null, bean); } Logger.i(TAG, "getJsonFromServer---" + json); if (!TextUtils.isEmpty(json)) { if (isCache) { cacheJsonToFile(url, json, context); } return json; } return ""; } /** * 從本地緩存文件中取數據; * * @param url * @param context * @return 如果緩存存在且不過期,返回緩存數據,否則返回null; */ public static String getJsonFromFile(String url, Context context) { String result = ACache.get(context).getAsString(url); if (!TextUtils.isEmpty(result)) { return result; } return null; } /** * 從SharedPreferences中取緩存數據; * * @param url * @param context * @return */ public static String getJsonFromSharedPref(String url, Context context) { Logger.i(TAG, "getJsonFromSharedPref---" + url); SharedPreferences sp = context.getSharedPreferences(CACHE_JSON_SP, Context.MODE_PRIVATE); String json = ""; if (sp.contains(url)) { json = sp.getString(url, ""); } return json; } /** * 將Server返回的json字符串緩存到文件; * * @param url * @param json * @param context */ private static void cacheJsonToFile(String url, String json, Context context) { ACache.get(context).put(url, json, ACache.TIME_HOUR); } /** * 將Server返回的Json字符串緩存到本地SharedPreferences中; * * @param url * @param json * @param context */ private static void cacheJsonToSharedPref(String url, String json, Context context) { Logger.i(TAG, "cacheJsonToSharedPref---" + url); SharedPreferences sp = context.getSharedPreferences(CACHE_JSON_SP, Context.MODE_PRIVATE); Editor editor = sp.edit(); editor.putString(url, json); editor.commit(); Logger.i(TAG, "---cache Json to SharedPref end---"); } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!