android-async-http提供了异步和基于回调的下载库,地址:http://loopj.com/android-async-http/。提供了多中回调处理类,用于处理不同场景。我使用的比较多的是如下几个:
1、基类,主要需要覆盖onSuccess和onFailure方法
java.lang.Object com.loopj.android.http.AsyncHttpResponseHandler All Implemented Interfaces: ResponseHandlerInterface
2、 BinaryHttpResponseHandler,在onSuccess方法中返回byte[]数据类型
java.lang.Object com.loopj.android.http.AsyncHttpResponseHandler com.loopj.android.http.BinaryHttpResponseHandler
3、 FileAsyncHttpResponseHandlerjava.lang.Object com.loopj.android.http.AsyncHttpResponseHandler com.loopj.android.http.FileAsyncHttpResponseHandler
其构造方法如下:
因此,只需将文件对象传入构造函数,下载时就能自动写入到本地文件中,非常方便。
FileAsyncHttpResponseHandler(android.content.Context context)
Obtains new FileAsyncHttpResponseHandler against context with target being temporary fileFileAsyncHttpResponseHandler(java.io.File file)
Obtains new FileAsyncHttpResponseHandler and stores response in passed fileFileAsyncHttpResponseHandler(java.io.File file, boolean append)
Obtains new FileAsyncHttpResponseHandler and stores response in passed file