这是Volley框架解析的最后一栏,Response类的源码总共不到100行,我就直接全部贴出来了:
public class Response<T> {
/** Callback interface for delivering parsed responses. */
public interface Listener<T> {
/** Called when a response is received. */
public void onResponse(T response);
}
/** Callback interface for delivering error responses. */
public interface ErrorListener {
/**
* Callback method that an error has been occurred with the
* provided error code and optional user-readable message.
*/
public void onErrorResponse(VolleyError error);
}
/**
* Returns a successful response containing the parsed result.
*
* 成功
*/
public static <T> Response<T> success(T result, Cache.Entry cacheEntry) {
return new Response<T>(result, cacheEntry);
}
/**
* Returns a failed response containing the given error code and an optional
* localized message displayed to the user.
*
* 失败
*/
public static <T> Response<T> error(VolleyError error) {
return new Response<T>(error);
}
/** Parsed response, or null in the case of error. 返回数据,错误的情况下为空*/
public final T result;
/** Cache metadata for this response, or null in the case of error. 缓存数据,错误的情况下为空*/
public final Cache.Entry cacheEntry;
/** Detailed error information if <code>errorCode != OK</code>. 错误error*/
public final VolleyError error;
/** True if this response was a soft-expired one and a second one MAY be coming.命中过期缓存标志*/
public boolean intermediate = false;
/**
* Returns whether this response is considered successful.
*
* 是否成功
*/
public boolean isSuccess() {
return error == null;
}
private Response(T result, Cache.Entry cacheEntry) {
this.result = result;
this.cacheEntry = cacheEntry;
this.error = null;
}
private Response(VolleyError error) {
this.result = null;
this.cacheEntry = null;
this.error = error;
}
}
这里有一些我们非常熟悉的东西,比如Listener.onResponse成功回调,ErrorListener.onErrorResponse失败回调,框架内部用于通知的方法,success与error等等,关于这两个方法的如何工作,我们已经在前面说的很清楚了,如果还不明白的同学可以回头看看。
至此整篇Volley框架的解析全部结束,可能有同学会认为最后的Request与Response解析的太过肤浅,虽然它们是Volley中十分重要的两个类,但连接它们的主线在前面我们就已经说完了,这里只是对它们进行补充,单说方法却没有一根线能贯通全局,理解起来就会十分抽象,这也是我一开始不说它们的原因。
愿大家能在这篇博客中收获一二!
最后附上Volley的源码,供大家学习参考,点击下载