GalHttprequest 是一个android平台上一个轻量级的http网络请求及缓存框架。
当前GalHttpRequest支持以下功能:
同步请求Stirng、InputStream、Bitmap;
异步请求String、InputStream、Bitmap;支持回调接口;
支持异步下载文件,提供监听进度回调接口;
支持缓存参数设置;
支持多线程及队列请求;
自动适配移动、联通、电信wap代理;
支持快捷post请求;
项目如图:
效果如图:
主要部分就十一个目标文件,其定义如下:
//解析日期的日期和时间(RFC822和W3CDateTime的格式字符串)
public class DateParser
主要部分就十一个目标文件,其定义如下:
//解析日期的日期和时间(RFC822和W3CDateTime的格式字符串)
public class DateParser
//下载参数 成员下载路径 、在状态栏的标题、保存文件名等等
public class GalDownloadParams
//下载任务 使用了同步任务
public class GalDownLoadTask extends
AsyncTask<GalDownloadParams, Integer, Void>
//自定义字符串单元 有UTF编解码、获得sd路径、字符串分割、半角转全角等操作
public class GalStringUtil
//自定义url
public class GALURL
//输入单元
public class LogUtil
//构造MD5值
public class MD5
//下载任务监听 加载进程 加载完成 加载失败 加载取消
public class SimpleDownLoadTaskListener implements GalDownLoadTaskListener
/*
* 定制自己的HttpClient,确保唯一实例,提供全局访问接口
* 自定义timeout时间等参数
*/
public class MyHttpClient
//http请求 构造请求参数、请求头、处理返回信息等操作
//使用线程池,来重复利用线程,优化内存
public class GalHttpRequest
//线程池的定义
private static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors
.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
//Executors的静态方法newCachedThreadPool, newFixedThreadPool, newScheduledThreadPool
//所返回的线程池都是ThreadPoolExecutor对象或者其子类对象.
//ThreadPoolExecutor提供了多种配置, 可以根据实际定制合适的线程池.
测试代码:
//初始化单击监听
private void initListener()
{
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
String item = (String) parent.getAdapter().getItem(position);
int type = 0;
if ("同步请求InputStream".equalsIgnoreCase(item))
{
type = RequestType.SYNC_REQUESTIS;
} else if ("同步请求String".equalsIgnoreCase(item))
{
type = RequestType.SYNC_REQUESTSTRING;
} else if ("同步请求Bitmap".equalsIgnoreCase(item))
{
type = RequestType.SYNC_REQUESTBITMAP;
} else if ("异步请求InputStream".equalsIgnoreCase(item))
{
type = RequestType.ASYN_REQUESTIS;
} else if ("异步请求String".equalsIgnoreCase(item))
{
type = RequestType.ASYN_REQUESTSTRING;
} else if ("异步请求Bitmap".equalsIgnoreCase(item))
{
type = RequestType.ASYN_REQUESTBITMAP;
} else if ("组装http参数".equalsIgnoreCase(item))
{
type = RequestType.ASYN_EASYPARAMS;
} else if ("Post内容".equalsIgnoreCase(item))
{
type = RequestType.ASYN_EASYPOST;
}
Intent intent = new Intent(GalHttpRequestDemoActivity.this,
RequestActivity.class);
intent.putExtra("type", type);
startActivity(intent);
}
});
}
/**
* @Title: startRequest
* @Description:TODO(这里用一句话描述这个方法的作用)
* @param @param type 传入参数名字
* @return void 返回类型
* @date 2012-4-23 下午11:37:16
* @throw
*/
private void startRequest(int type)
{
GalHttpRequest request;
textView.setVisibility(View.GONE);
imageView.setVisibility(View.GONE);
switch (type)
{
case RequestType.SYNC_REQUESTIS:
{
// 同步请求InputStream
title.setText("同步请求InputStream");
request = GalHttpRequest.requestWithURL(this, PATH_INPUTSTREAM);
// 如果不检测缓存,则设置:
// request.setCacheEnable(false);
// 必须在调用startXXX()函数之前设置
// 返回的缓存已经是ufferedInputStream类型
InputStream is = request.startSynchronous();
textView.setVisibility(View.VISIBLE);
if (is != null)
{
textView.setText(is.toString());
}
break;
}
case RequestType.SYNC_REQUESTSTRING:
{
// 同步请求String
title.setText("同步请求String");
request = GalHttpRequest.requestWithURL(this, PATH_STRING);
// 根据服务器返回的状态读取内容,如果服务器内容没有改变,则直接读取缓存内容,如果服务器内容已经修改,则从服务器拉取数据
// 并刷新缓存内容
String string = request.startSyncRequestString();
textView.setText(string);
textView.setVisibility(View.VISIBLE);
break;
}
case RequestType.SYNC_REQUESTBITMAP:
{
// 同步请求Bitmap
title.setText("同步请求Bitmap");
Header header = new BasicHeader("Accept-Language", "zh-cn,zh;q=0.5");
// 支持添加自定义的Http Header请求
request = GalHttpRequest.requestWithURL(this, PATH_BITMAP,
new Header[]
{ header });
// 请求Bitmap,由于图片基本上不改变,因此如果存在缓存,则直接从缓存读取
Bitmap bitmap = request.startSyncRequestBitmap();
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
break;
}
case RequestType.ASYN_REQUESTIS:
{
// 异步请求InputStream
title.setText("异步请求InputStream");
request = GalHttpRequest.requestWithURL(this, PATH_INPUTSTREAM);
// 必须先设置回调函数,否则调用异步请求无效
request.setListener(new GalHttpRequestListener()
{
@Override
public void loadFinished(final InputStream is, boolean fromcache)
{
// 注意,由于返回的是InputStream,一般情况都需要长时间操作,所以,回调函数是在子线程调用
// 因此使用handler
handler.post(new Runnable()
{
@Override
public void run()
{
textView.setText(is.toString());
textView.setVisibility(View.VISIBLE);
}
});
}
@Override
// 请求失败时,有可能可以从缓存里面读取数据返回
public void loadFailed(final HttpResponse respone,
InputStream cacheInputStream)
{
handler.post(new Runnable()
{
@Override
public void run()
{
textView.setText(respone.toString());
textView.setVisibility(View.VISIBLE);
}
});
}
});
request.startAsynchronous();
break;
}
case RequestType.ASYN_REQUESTSTRING:
{
title.setText("异步请求String");
// 异步请求String
request = GalHttpRequest.requestWithURL(this, PATH_STRING);
// 第一次调用startAsynRequestString或者startAsynRequestBitmap必须在主线程调用
// 因为只有在主线程中调用才可以初始化GalHttprequest内部的全局句柄Handler
request.startAsynRequestString(new GalHttpLoadTextCallBack()
{
@Override
public void textLoaded(String text)
{
// 该部分允许于UI线程
textView.setText(text);
textView.setVisibility(View.VISIBLE);
}
});
break;
}
case RequestType.ASYN_REQUESTBITMAP:
{
title.setText("异步请求Bitmap");
// 异步请求Bitmap
request = GalHttpRequest.requestWithURL(this, PATH_BITMAP);
request.startAsynRequestBitmap(new GalHttpLoadImageCallBack()
{
@Override
public void imageLoaded(Bitmap bitmap)
{
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
}
});
break;
}
case RequestType.ASYN_EASYPARAMS:
{
// 异步组装参数
title.setText("组装http参数");
// 交给GalHttprequest自动组装url中的参数
NameValuePair feedPair = new BasicNameValuePair("p", "51");
request = GalHttpRequest.requestWithURL(this, PATH_WITHPARAMS,
feedPair);
request.startAsynRequestString(new GalHttpLoadTextCallBack()
{
@Override
public void textLoaded(String text)
{
// 该部分允许于UI线程
textView.setText(text);
textView.setVisibility(View.VISIBLE);
}
});
break;
}
case RequestType.ASYN_EASYPOST:
{
// 异步post 数据给服务器
title.setText("异步post 数据给服务器");
// 交给GalHttprequest自动组装url中的参数
request = GalHttpRequest.requestWithURL(this, PATH_POSTCONTENT);
// 设置post内容
request.setPostValueForKey("name", "qiuscut");
request.startAsynRequestString(new GalHttpLoadTextCallBack()
{
@Override
public void textLoaded(String text)
{
// 该部分允许于UI线程
textView.setText("在这里post应该是无效的,因为当前url不支持post");
textView.setVisibility(View.VISIBLE);
}
});
break;
}
default:
finish();
return;
}
}
学习的目标是成熟!~~~~~
该项目在oschina的主页 http://www.oschina.net/p/galhttprequest