自定义Request:
我们先看下Request,是所有网络请求的基类。
支持的请求方式,包含post,get等请求方式,在定义请求方式中可以自己设定。
/**
* Supported request methods.
*/
public interface Method {
int DEPRECATED_GET_OR_POST = -1;
int GET = 0;
int POST = 1;
int PUT = 2;
int DELETE = 3;
int HEAD = 4;
int OPTIONS = 5;
int TRACE = 6;
int PATCH = 7;
}
构造方法,如果想修改请求方法就在这里设定:
/**
* Creates a new request with the given URL and error listener. Note that
* the normal response listener is not provided here as delivery of responses
* is provided by subclasses, who have a better idea of how to deliver an
* already-parsed response.
*
* @deprecated Use {@link #Request(int, String, com.android.volley.Response.ErrorListener)}.
*/
@Deprecated
public Request(String url, Response.ErrorListener listener) {
this(Method.DEPRECATED_GET_OR_POST, url, listener);
}
/**
* Creates a new request with the given method (one of the values from {@link Method}),
* URL, and error listener. Note that the normal response listener is not provided here as
* delivery of responses is provided by subclasses, who have a better idea of how to deliver
* an already-parsed response.
*/
public Request(int method, String url, Response.ErrorListener listener) {
mMethod = method;
mUrl = url;
mErrorListener = listener;
setRetryPolicy(new DefaultRetryPolicy());
mDefaultTrafficStatsTag = TextUtils.isEmpty(url) ? 0: Uri.parse(url).getHost().hashCode();
}
监听处理,将网络请求交给你的监听处理。
/**
* Subclasses must implement this to perform delivery of the parsed
* response to their listeners. The given response is guaranteed to
* be non-null; responses that fail to parse are not delivered.
* @param response The parsed response returned by
* {@link #parseNetworkResponse(NetworkResponse)}
*/
abstract protected void deliverResponse(T response);
处理请求的数据,返回的是数据是字节数组,然后根据你的需要处理数组:
/**
* Subclasses must implement this to perform delivery of the parsed
* response to their listeners. The given response is guaranteed to
* be non-null; responses that fail to parse are not delivered.
* @param response The parsed response returned by
* {@link #parseNetworkResponse(NetworkResponse)}
*/
abstract protected void deliverResponse(T response);
以上的方法是重写中,必要涉及到的,下面我们来看下StringRequest的代码:
public class StringRequest extends Request<String> {
private final Listener<String> mListener;
public StringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
ImageRequest ,JsonArrayRequest等,形式相同,所以我们自定义的请求也可以“借鉴”下这种写法。
public class DownLoadRequest extends Request {
private Listener listener;
/** 自定义的类 */
private SimpleKeyBean simBean;
/**
* 构造:自定义请求参数
*
* @param url
* @param sim
* @param listener
* @param errorlistener
*/
public DownLoadRequest(String url, SimpleKeyBean sim, Listener listener,
ErrorListener errorlistener) {
super(Method.GET, url, errorlistener);
// TODO Auto-generated constructor stub
this.listener = listener;
this.simBean = sim;
}
@Override
protected void deliverResponse(Object res) {
// TODO Auto-generated method stub
listener.onResponse(res);
}
@Override
protected Response parseNetworkResponse(NetworkResponse response) {
// TODO Auto-generated method stub
DownBMBean bean;
try {
// 文件路径
String path = DataProcess.dirPackage() + "/" + simBean.getSinger()
+ "-" + simBean.getSong() + ".mp3";
// 将字节数组保存为文件
DataProcess.saveByteFile(response.data, path);
// 创建返回的对象
bean = new DownBMBean("suc", path);
// 返回数据
return Response.success(bean,
HttpHeaderParser.parseCacheHeaders(response));
} catch (Exception e) {
// TODO: handle exception
return Response.error(new ParseError(e));
}
}
}
使用:
RequestQueue reQueue = Volley.newRequestQueue(this);
//传递请求的参数
DownLoadRequest request = new DownLoadRequest(path, simBean,
new Response.Listener() {
public void onResponse(Object bean) {
// TODO Auto-generated method stub
//获取返回的对象
dwBean = (DownBMBean) bean;
insHandler.sendEmptyMessage(1);
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
insHandler.sendEmptyMessage(2);
}
});
reQueue.add(request);