org.xutils.http.loader.Loader&LoaderFactory

 

package org.xutils.http.loader;import android.text.Tex...





package org.xutils.http.loader;
import android.text.TextUtils;
import org.xutils.cache.DiskCacheEntity;
import org.xutils.cache.LruDiskCache;
import org.xutils.http.ProgressHandler;
import org.xutils.http.RequestParams;
import org.xutils.http.request.UriRequest;
import java.io.InputStream;
import java.util.Date;
/**
* @author 注释者:王教成
* @version 注释版:1.0.0
* 加载器抽象类
*/
public abstract class Loader {

protected RequestParams params;//声明请求参数

protected ProgressHandler progressHandler;//声明进度操作器

/**
    * 设置请求参数
    * @param params 请求参数
    */
   public void setParams(final RequestParams params) {

this.params = params;

}

/**
    * 设置进度操作器
    * @param callbackHandler 进度操作器
    */
   public void setProgressHandler(final ProgressHandler callbackHandler) {

this.progressHandler = callbackHandler;

}

/**
    * 保存字符串缓存
    * @param request Uri请求参数
    * @param resultStr 结果字符串
    */
   protected void saveStringCache(UriRequest request, String resultStr) {

if (!TextUtils.isEmpty(resultStr)) {

DiskCacheEntity entity = new DiskCacheEntity();//如果结果字符串非空,创建磁盘缓存实体

entity.setKey(request.getCacheKey());//用Uri请求参数获取缓存键,设置缓存键

entity.setLastAccess(System.currentTimeMillis());//获取当前毫秒时间,设置最近访问时间

entity.setEtag(request.getETag());//用Uri请求参数获取电子标签,设置电子标签

entity.setExpires(request.getExpiration());//用Uri请求参数获取截止日期,设置截止日期

entity.setLastModify(new Date(request.getLastModified()));//用Uri请求参数获取最后修改时间,设置最后修改时间

entity.setTextContent(resultStr);//以结果字符串,设置文本内容

LruDiskCache.getDiskCache(request.getParams().getCacheDirName()).put(entity);

}//用最近最少使用算法磁盘缓存,获取磁盘缓存,放入磁盘缓存实体(以Uri请求参数获取参数,再获取缓存目录名称作为参数)

}

/**
    * 创建新实例
    * @return 返回泛型加载器
    */
   public abstract Loader newInstance();

/**
    * 通过输入流加载
    * @param in 输入流
    * @return 返回泛型
    * @throws Throwable 抛出异常
    */
   public abstract T load(final InputStream in) throws Throwable;

/**
    * 通过Uri请求参数加载
    * @param request Uri请求参数
    * @return 返回泛型
    * @throws Throwable 抛出异常
    */
   public abstract T load(final UriRequest request) throws Throwable;

/**
    * 从缓存实体加载
    * @param cacheEntity 磁盘缓存实体
    * @return 返回泛型
    * @throws Throwable 抛出异常
    */
   public abstract T loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable;

/**
    * 保存到缓存实体
    * @param request Uri请求参数
    */
   public abstract void save2Cache(final UriRequest request);
}
package org.xutils.http.loader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import java.io.File;
import java.lang.reflect.Type;
import java.util.HashMap;
/**
* @author 注释者:王教成
* @version 注释版:1.0.0
* 加载器工厂
*/
public final class LoaderFactory {

private LoaderFactory() {

}//构造器

/**
    * key:加载类型
    */
   private static final HashMap converterHashMap = new HashMap();//创建转换器HashMap,key为加载类型,value为加载器

static {

converterHashMap.put(JSONObject.class, new JSONObjectLoader());//放入JSON对象类及其加载器

converterHashMap.put(JSONArray.class, new JSONArrayLoader());//放入JSON数组类及其加载器

converterHashMap.put(String.class, new StringLoader());//放入字符串类及其加载器

converterHashMap.put(File.class, new FileLoader());//放入文件类及其加载器

converterHashMap.put(byte[].class, new ByteArrayLoader());//放入字节数组类及其加载器

BooleanLoader booleanLoader = new BooleanLoader();//创建布尔加载器

converterHashMap.put(boolean.class, booleanLoader);//放入布尔类及其加载器

converterHashMap.put(Boolean.class, booleanLoader);//放入布尔包装类及其加载器

IntegerLoader integerLoader = new IntegerLoader();//创建整形加载器

converterHashMap.put(int.class, integerLoader);//放入整形类及其加载器

converterHashMap.put(Integer.class, integerLoader);//放入整形包装类及其加载器

}//静态初始化块

/**
    * 获取加载器
    * @param type 类型对象
    * @param params 请求参数
    * @return 返回加载器
    */
   @SuppressWarnings("unchecked")

public static Loader getLoader(Type type, RequestParams params) {

Loader result = converterHashMap.get(type);//从转换器HashMap获取加载器

if (result == null) {

result = new ObjectLoader(type);//如果加载器为空,创建对象加载器

} else {

result = result.newInstance();//如果加载器非空,用加载器创建新实例

}

result.setParams(params);//以请求参数设置加载器参数

return result;

}

/**
    * 注册加载器
    * @param type 类型对象
    * @param loader 加载器
    * @param 加载器泛型
    */
   public static

void registerLoader(Type type, Loader loader) {

converterHashMap.put(type, loader);//放入类型对象和加载器键值对

}
}


    关注 技术分析师


微信扫一扫关注公众号

0 个评论

要回复文章请先登录注册