开发pad应用时,通常使用v4的Fragment,使用v4包本身的LoaderManager:
使用v4中的LoaderManager的步骤如下
1.新建Fragment时要实现接口LoaderCallbacks
android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>
2.在onCreate方法中调用initLoader方法
<Cursor> Loader<Cursor> android.support.v4.app.LoaderManager.initLoader(int arg0, Bundle arg1, LoaderCallbacks<Cursor> arg2)
3.实现接口LoaderCallbacks的抽象方法
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1); public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1); public void onLoaderReset(Loader<Cursor> arg0);
新建TvieRequest类,封装了从网络获取数据大部分细节,提供从网络获取数据大部分功能。
TvieRequest类分以下几个部分:
1. 定义变量
private String requestUrl; private Map<String, String> paramValues; private ICallback callback; private NetUtil.HttpMethod httpMethod; private Class<?> resultType; private int requestCode; private int resultCode;
2. 定义常量
public static final int RESULT_SUCCESS = 1; public static final int RESULT_ERROR = 2; public static final int RESULT_EMPTY = 3; public static final int RESULT_CANCEL = 4;
3. 定义接口
public static interface ICallback { public void onRequestFinished(int requestCode, int resultCode, IVideoResult result); public void onRequestStarted(int requestCode); };
4. 定义异步任务类
private class MyAsyncTask extends AsyncTask<Void, Void, IVideoResult> { @Override protected void onPreExecute() { callback.onRequestStarted(requestCode); } @Override protected void onPostExecute(IVideoResult result) { callback.onRequestFinished(requestCode, resultCode, result); } private String cache(String data) { File cacheFile = CacheUtils.getCacheFile(requestUrl, paramValues); if (data == null) { data = CacheUtils.getDataFromCache(cacheFile); } else { CacheUtils.updateCache(cacheFile, data); } return data; } @Override protected IVideoResult doInVoid... params) { IVideoResult result = null; String response = null; JSONObject object = null; response = NetUtil.request(paramValues, requestUrl, httpMethod); if (httpMethod == NetUtil.HttpMethod.GET) { response = cache(response); } if (response == null) { resultCode = RESULT_ERROR; return null; } else if (response.length() == 0) { resultCode = RESULT_EMPTY; return null; } object = JSONObject.parse(response); result = (IVideoResult) ReflectUtils.getNewInstance(resultType.getName()); result.parseJson(object); if(result != null && result.isSuccess()) resultCode = RESULT_SUCCESS; else resultCode = RESULT_ERROR; return result; } }
5. 定义构造方法,创建TvieRequest实例
public TvieRequest(String requestUrl, Map<String, String> paramValues, ICallback callback, HttpMethod httpMethod, Class<?> resultType, int requestCode) { this.requestUrl = requestUrl; this.paramValues = paramValues; this.callback = callback; this.httpMethod = httpMethod; this.resultType = resultType; this.requestCode = requestCode; }
6. 定义execute方法,调用MyAsyncTask的execute方法
private void execute() { new MyAsyncTask().execute(); }
7. 定义三个静态方法
public static void post(int requestCode, Class<?> resultType, ICallback callback, String requestUrl, Map<String, String> params) { new TvieRequest(requestUrl, params, callback, NetUtil.HttpMethod.POST, resultType, requestCode).execute(); } public static void get(int requestCode, Class<?> resultType, ICallback callback, String requestUrl, Map<String, String> params) { new TvieRequest(requestUrl, params, callback, NetUtil.HttpMethod.GET, resultType, requestCode).execute(); } public static void delete(int requestCode, Class<?> resultType, ICallback callback, String requestUrl, Map<String, String> params) { new TvieRequest(requestUrl, params, callback, NetUtil.HttpMethod.DELETE, resultType, requestCode).execute(); }