我封裝的這個框架暫且就叫它Wonder框架吧, Wonder驚奇的意思,意思是讓人眼前一亮。改變了原先咱們喜歡重複複製粘貼代碼的壞習慣,浪費了時間的同時還很差管理代碼。java
廢話不說,我們先看看簡單架構:android
AWonder企業框架算法
AWonder是一個android的sqlite orm 和 ioc 框架。同時封裝了android中的http,多任務斷點文件下載和一些經常使用的封裝。sql
使用AsyncHttpClient進行Json解析:json
1>、第一步:咱們須要寫個內部類或者是普通的專門用於網絡請求的類,我喜歡將全部的網絡請求類放在一個包中,這個看我的愛好。數組
public class HttpClientFile extends HttpRestClient implements HttpRestClient.BaseControlInterface{ }
2>、第二步:實現httpRestClient中的全部方法緩存
public HttpClientFile(Context context) { super(context); } /** * 請求服務器的IP */ @Override public String url() { return null; } /** * 文件上傳的參數配置 */ @Override public SimpleMultipartEntity setMultipartEntityMessage() { return null; } /** * 服務器鏈接失敗操做 */ @Override public void onFailureMessage(Throwable error, String content) { } /** * 網絡請求成功以後進行的操做 */ @Override public void onSucceMessage(int statusCode, String content) { // TODO Auto-generated method stub } /** * 請求服務器參數的配置 */ @Override public RequestParams setParamsMessage() { return null; } }
須要注意的是:我經過接口的形式封裝的這個類有一個地方須要注意:若是你只是請求網絡返回json或者是xml的話那你只須要在url方法中填寫你的ip,而後在setParamsMessage方法中將請求參數拼接就行,而後在返回的onSuccessMessage或者是onFailureMessage方法中進行相關的操做。服務器
若是你須要上傳文件的話,那你須要在「setMultipartEntityMessage」方法中進行參數拼接,而且在此處添加文件流。這樣就能夠實現文件傳輸了,具體有例子能夠證實:網絡
使用例子:架構
public class HttpClientFile extends HttpRestClient implements HttpRestClient.BaseControlInterface{ public HttpClientFile(Context context) { super(context); } @Override public String url() { //請求服務器的IP return null; } /** * 文件上傳的參數配置 */ @Override public SimpleMultipartEntity setMultipartEntityMessage() { //文件上傳的參數配置 SimpleMultipartEntity simpleentity = new SimpleMultipartEntity(); //在表單中,增長第一個input type='file'元素,name=‘uploadedfile’ File file2 = new File(srcPath2); try { simpleentity.addPart("img", file2.getName(), new FileInputStream(file2), true); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return simpleentity; } /** * 服務器鏈接失敗操做 */ @Override public void onFailureMessage(Throwable error, String content) { //服務器鏈接失敗操做 } /** * 網絡請求成功以後進行的操做 */ @Override public void onSucceMessage(int statusCode, String content) { // TODO Auto-generated method stub } /** * 請求服務器參數的配置 */ @Override public RequestParams setParamsMessage() { RequestParams params = new RequestParams(); params.put("mac", "00:d3:4b:10:00:30"); params.put("sn", "00:d3:4b:10:00:30"); params.put("type", "1"); params.put("key", "5d10372f217d59a4844bb6bb12be6de1"); return params; } }
使用XmlUtil的PULL進行XML解析:
1>、第一步:XmlUtil xmlUtil= new XmlUtil();
2>、第二步:實現xmlUtil中的方法,包括(xmlUtil.getXmlList(); xmlUtil.getXmlObject())
使用例子:
//實例化一個List<JavaBean>:
<?xml version="1.0" encoding="UTF-8"?> <person personid="0000000" id="1010" > <people people id="002"> <name>張三</name> <tel>12345</tel> </people> <people people id="002"> <name>李四</name> <tel>678910</tel> </people> </person>
//實例化一個List<JavaBean>:
List<People> mList = new ArrayList<People>(); XmlUtil xmlUtil = new XmlUtil(); People people = new People(); mList = xmlUtil.getXmlList(url, people, "people"); 1) mList = xmlUtil.getXmlObject(url, people);
2)
AWonder執行的是緩存,而後SD卡,最後網絡
下載的順序。
使用例子:方法一
/** * 須要在此處添加 */ BitmapUtils bitmapUtils = BitmapHelp.getBitmapUtils(getApplicationContext(), Environment.getExternalStorageDirectory() + "/ai"); bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher); bitmapUtils.configDefaultLoadFailedImage(R.drawable.test_list_view_bg); bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565); // bitmapUtils.configMemoryCacheEnabled(false); //是否設置內存緩存 // bitmapUtils.configDiskCacheEnabled(false); //是否設置SD卡文件緩存 bitmapUtils.configDefaultAutoRotation(true); //設置是否旋轉
最後調用
bitmapUtils.display(imageView, url, new CustomBitmapLoadCallBack(holder));
方法二
ABitmap bitmap = ABitmap.create(getApplicationContext(), diskCachePath);
bitmap.display(imageView, url);
經過CacheG 和 CahceP中的getCache()和putCache()方法,您只須要調用一句話就能實現緩存的存取。
調用的方法:
@Override
public void putCacheInterG(Context context, String spName, String key,
int value) {
// TODO Auto-generated method stub
super.putCacheInterG(context, spName, key, value);
}
@Override
public void putCacheStringG(Context context, String spName, String key,
String value) {
// TODO Auto-generated method stub
super.putCacheStringG(context, spName, key, value);
}
@Override
public void putCacheLongG(Context context, String spName, String key,
long value) {
// TODO Auto-generated method stub
super.putCacheLongG(context, spName, key, value);
}
@Override
public void putCacheBooleanG(Context context, String spName, String key,
Boolean value) {
// TODO Auto-generated method stub
super.putCacheBooleanG(context, spName, key, value);
}
注:getacheInteger取值時默認爲0
getCacheString取值時默認爲null
getCacheBoolean取值時默認爲true
Downloadmanager下載文件:
DownloadManager downloadManager = DownloadManager.getDownloadManager(getApplicationContext());
downloadManager.configDownloadPath("FilePath"); //配置下載完的文件存儲的位置
downloadManager.addHandler(url); //添加要下載文件的URL
downloadManager.setDownLoadCallback(new DownLoadCallback() {
@Override
public void onSuccess(String url) {
Toast.makeText(DownLoadDemoActivity.this, "數據下載完成,請查看!", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoading(String url, long totalSize, long currentSize,
long speed) {
super.onLoading(url, totalSize, currentSize, speed);
Toast.makeText(DownLoadDemoActivity.this, "正在下載數據.....", Toast.LENGTH_SHORT).show();
}
});
/**
* 寫文本文件
* 在Android系統中,文件保存在 /data/data/PACKAGE_NAME/files 目錄下
* @param context
* @param msg
*/
FileUtils.write(Context context, String fileName, String content)) //寫文件
/**
* 讀取文本文件
* @param context
* @param fileName //文件名
* @return
*/
FileUtils.read(Context context, String fileName ) //讀文件
/**
* @param folderPath //文件夾名稱
* @param fileName //文件名
* @return
*/
FileUtils.createFile(String folderPath, String fileName ) //讀文件
/**
* 根據文件絕對路徑獲取文件名
* @param filePath
* @return
*/
FileUtils.getFileName( String filePath )
/**
* 檢查文件是否存在
* @param name
* @return
*/
FileUtils.checkFileExists(String name)
/**
* 計算SD卡的剩餘空間
* @return 返回-1,說明沒有安裝sd卡
*/
FileUtils.getFreeDiskSpace()
/**
* 檢查是否安裝SD卡
* @return
*/
FileUtils.checkSaveLocationExists()
/**
* 刪除文件
* @param fileName
* @return
*/
FileUtils.deleteFile(String fileName)
/**
* 檢測網絡是否鏈接(包括3G和Wifi網)
* @return
*/
Boolean bool = CheckNet.checkNet(Context context);
/**判斷WIFI網絡是否打開
* @param activity
* @return
*/
Boolean bool = CheckNet.isWifiActive(Activity activity)
/**
* 判斷網絡是否打開
* @param activity
* @return
*/
Boolean bool = CheckNet.isWifiActive(Context context)
/**
* 判斷是否有網絡鏈接,若是沒有聯網就彈出Dialog並提示
* @param activity
*/
Boolean bool = CheckNet.CheckNetworkState(Activity activity)
// 解密數據
CyptoThreeDS.decrypt(String message, String key)
//加密數據
CyptoThreeDS.encrypt(String message, String key)
/**
* MD5加密
* @param content
*/
CyptoUtils.getMD5(String content)
/**
* DES算法,加密
* @param data 待加密字符串
* @param key 加密私鑰,長度不可以小於8位
* @return 加密後的字節數組,通常結合Base64編碼使用
* @throws InvalidAlgorithmParameterException
* @throws Exception
*/
CyptoUtils.encode(String key,String data)
/**
* DES算法,解密
* @param data 待解密字符串
* @param key 解密私鑰,長度不可以小於8位
* @return 解密後的字節數組
* @throws Exception 異常
*/
CyptoUtils.decode(String key,String data)
/**
* 獲取手機ip start
*/
public static String getLocalIpAddress()
/**
* 獲取手機ip method-2
* @return
*/
public static String getLocalIpAddress2()
/**
* mac地址 start
*/
public static String getLocalMacAddress()
/**
* 獲取Android手機中SD卡存儲信息 獲取剩餘空間
* @param Exception == SD卡不存在
* @return
*/
public static long getSDCardInfo(String Exception)
/**
* 獲取Android手機中SD卡存儲信息 獲取剩餘空間
* @param Exception == SD卡不存在
* @return
*/
public static long getSDCardInfo1(String Exception)
/**
* 獲取手機可用內存
* @return
*
*/
public static String getSystemMemory() {
}
/**
* 獲取手機總內存
* @return
*
*/
public static String getSystemMemory1() {
}
/**
* 獲取手機CPU信息
* @param tag ------- tag = 1(獲取cpu型號) ----------tag = 2(獲取cpu型號)
* @return
*/
private static String getCpuInfo(String tag)
/**
* 手機IMEI
*/
private static String getImei()
/**
* 獲取Wifi的MAC地址,須要開啓才能獲取
* @return
*/
public static String getLocalWifiMacAddressT()
/**
* 獲取Wifi的MAC地址,只須要開啓過之後就都能獲取
* @return
*/
public static String getLocalNetAddressMAC()
/**
* 獲取以太網MAC地址
* @return
*/
public static String getLocalIpAddressMAC()
/**
* 跳轉到下一個Activity
* @param 當前Activity
* @param 要跳轉的Activity
*/
public static void Go(Context context, Class<?> cls)
/**
* @param context
* @param cls
* @param bundle用於上一個activity向下一個activity傳參
*/
public static void Go(Context context, Class<?> cls, Bundle bundle)
public static void Go(Context context, Class<?> cls,
HashMap<String, Serializable> map)
Dialog:
tipDialog = new TipDialog(TipsActivity.this, R.style.Theme_dialog, R.layout.activity_dialog, 400, 200, 1111);
TOP:表明在最頂端
BOTTOM:表明在最下端
CENTER:表明在最中間
tipDialog.show(); //顯示
tipDialog.dismiss(); //消失
Toast:
TipToast.showToast(R.layout.toast_main, getApplicationContext(), 1111, 1101);
AWonder中還封裝了一些經常使用的類:好比:LogU.java(用於Log打印),電話和短信的封裝(intecept/phone../phonemsg../sms)等
最後是jar包和源碼