Volley 源码解析 StringRequest解析
Android Vollety是一个很有用的框架,所以想借鉴前人思想,分析这个源代码。
参考: http://blog.csdn.net/crazy__chen/article/details/46486123
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);
this.mListener = listener;
}
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(0, url, listener, errorListener);
}
protected void deliverResponse(String response) {
this.mListener.onResponse(response);
}
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException var4) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
这就是StringRequest源码,其实比较短,关键还是 继承了Request
现在着重介绍Request;
分开介绍:
/**
*默认编码格式
*/
private static final String DEFAULT_PARAMS_ENCODING = "UTF-8";
private final MarkerLog mEventLog;
private final int mMethod;//请求方式,常见的get,post
private final String mUrl;//请求地址
private final int mDefaultTrafficStatsTag;// 流量统计标签
private final ErrorListener mErrorListener;
private Integer mSequence;//请求序号,用于fifo算法
private RequestQueue mRequestQueue;//请求所在的请求队列 ,这也是一个重点,有时间再写一篇
private boolean mShouldCache;//是否使用缓存响应请求
private boolean mCanceled;//该请求是否被取消
private boolean mResponseDelivered;
private long mRequestBirthTime;//请求产生时间
private static final long SLOW_REQUEST_THRESHOLD_MS = 3000L;
private RetryPolicy mRetryPolicy;//请求重试策略
private Entry mCacheEntry;
private Object mTag;
/** @deprecated */
public Request(String url, ErrorListener listener) {
this(-1, url, listener);
}
构造函数:
/**
* 构造函数:请求方式,创建新的请求(需要地址,错误监听器等参数)
* @param method
* @param url
* @param listener
*/
public Request(int method, String url, ErrorListener listener) {
this.mEventLog = MarkerLog.ENABLED?new MarkerLog():null;
this.mShouldCache = true;
this.mCanceled = false;
this.mResponseDelivered = false;
this.mRequestBirthTime = 0L;
this.mCacheEntry = null;
this.mMethod = method;
this.mUrl = url;
this.mErrorListener = listener;
this.setRetryPolicy(new DefaultRetryPolicy());
this.mDefaultTrafficStatsTag = TextUtils.isEmpty(url)?0:Uri.parse(url).getHost().hashCode();
}
/**
* 比较重要的function
* @param tag
*/
void finish(final String tag) {
if(this.mRequestQueue != null) {
this.mRequestQueue.finish(this);//请求完成
}
final long requestTime;
if(MarkerLog.ENABLED) {//如果开启了调试
requestTime = Thread.currentThread().getId();
if(Looper.myLooper() != Looper.getMainLooper()) {
//如果请求不是主线程
Handler mainThread = new Handler(Looper.getMainLooper());
mainThread.post(new Runnable() {
//在主线程中记录Log
public void run() {
Request.this.mEventLog.add(tag, requestTime);
Request.this.mEventLog.finish(this.toString());
}
});
return;
}
this.mEventLog.add(tag, requestTime);
this.mEventLog.finish(this.toString());
} else {//否则不开启调试
requestTime = SystemClock.elapsedRealtime() - this.mRequestBirthTime;
if(requestTime >= 3000L) {
VolleyLog.d("%d ms: %s", new Object[]{Long.valueOf(requestTime), this.toString()});
}
}
}
/**
* 请求优先级,比较定义,
* @param other
* @return
*/
public int compareTo(Request<T> other) {
Request.Priority left = this.getPriority();
Request.Priority right = other.getPriority();
return left == right?this.mSequence.intValue() - other.mSequence.intValue():right.ordinal() - left.ordinal();
}
这里面发现开始定义的几个函数都比较简单,所有源代码并不是我们所说的那么难看懂.
Request 不是很难理解,主演是一些基本属性的设置,比如重试策略,重向标记,自定义标记,还有错误信息,当然还有编码问题
Volley 源码解析 StringRequest解析的更多相关文章
- Volley 源码解析
Volley 源码解析 1. 功能介绍 1.1. Volley Volley 是 Google 推出的 Android 异步网络请求框架和图片加载框架.在 Google I/O 2013 大会上发布. ...
- 【安卓网络请求开源框架Volley源码解析系列】定制自己的Request请求及Volley框架源码剖析
通过前面的学习我们已经掌握了Volley的基本用法,没看过的建议大家先去阅读我的博文[安卓网络请求开源框架Volley源码解析系列]初识Volley及其基本用法.如StringRequest用来请求一 ...
- Volley 源码解析(转)
项目:Volley,分析者:grumoon,校对者:Trinea 本文为 Android 开源项目源码解析 中 Volley 部分项目地址:Volley,分析的版本:35ce778,Demo 地址:V ...
- Volley源码解析(三) 有缓存机制的情况走缓存请求的源码分析
Volley源码解析(三) 有缓存机制的情况走缓存请求的源码分析 Volley之所以高效好用,一个在于请求重试策略,一个就在于请求结果缓存. 通过上一篇文章http://www.cnblogs.com ...
- # Volley源码解析(二) 没有缓存的情况下直接走网络请求源码分析#
Volley源码解析(二) 没有缓存的情况下直接走网络请求源码分析 Volley源码一共40多个类和接口.除去一些工具类的实现,核心代码只有20多个类.所以相对来说分析起来没有那么吃力.但是要想分析透 ...
- MyBatis 源码分析 - 配置文件解析过程
* 本文速览 由于本篇文章篇幅比较大,所以这里拿出一节对本文进行快速概括.本篇文章对 MyBatis 配置文件中常用配置的解析过程进行了较为详细的介绍和分析,包括但不限于settings,typeAl ...
- InfluxDB源码目录结构解析
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 influxdata主目录结构 [root@localhost ...
- Spring源码入门——DefaultBeanNameGenerator解析 转发 https://www.cnblogs.com/jason0529/p/5272265.html
Spring源码入门——DefaultBeanNameGenerator解析 我们知道在spring中每个bean都要有一个id或者name标示每个唯一的bean,在xml中定义一个bean可以指 ...
- 笔记-twisted源码-import reactor解析
笔记-twisted源码-import reactor解析 1. twisted源码解析-1 twisted reactor实现原理: 第一步: from twisted.internet ...
- 鸿蒙内核源码分析(ELF解析篇) | 你要忘了她姐俩你就不是银 | 百篇博客分析OpenHarmony源码 | v53.02
百篇博客系列篇.本篇为: v53.xx 鸿蒙内核源码分析(ELF解析篇) | 你要忘了她姐俩你就不是银 | 51.c.h.o 加载运行相关篇为: v51.xx 鸿蒙内核源码分析(ELF格式篇) | 应 ...
随机推荐
- Java获取字符串里面的重复字符
public static void main(String[] args) { String word="天地玄黄宇宙洪荒" + "日月盈昃辰宿列张" + & ...
- oracle count 百万级 分页查询记要总数、总条数优化
oracle count 百万级 分页查询记录总数.总条数优化 oracle count 百万级 查询记录总数.总条数优化 最近做一个项目时,做分页时,发现分页查询速度很慢,分页我做的是两次查询,一次 ...
- python mysql备份脚本
#!/usr/bin/env python # encoding: utf-8 #@author: 东哥加油! #@file: pyinnobackup.py #@time: 2018/12/11 1 ...
- PHP 把字符转换为 HTML 实体 - htmlentities() 函数
定义和用法 htmlentities() 函数把字符转换为 HTML 实体. 语法 htmlentities(string,quotestyle,character-set) 参数 描述 string ...
- input动态模糊查询的实现方式
最近在用jQuery实现动态模糊查询的时候,找了挺久都没有找到像Vue.js的watch属性这么好用的动态模糊查询方法.就分享一下目前遇到的坑和可以实现动态查询的几种方式. 1.jQuery的chan ...
- 前端,基础选择器,嵌套关系.display属性,盒模型
基础选择器 1.统配选择器 控制html,body及body内跟显示相关的标签 *{ width:80px; height:80px; background-color:red; } 2.类选择器 以 ...
- 排序算法C语言实现——冒泡排序
/*冒泡O(n^2)*//*原理: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数 ...
- Programming Python 3rd Edition 第三版 pdf chm下载
Programming Python 作为一款经典系列书籍 非常的耐看 建议有志于学习python的童鞋好好看看 网上 Programming Python第四版的 pdf 下载非常容易 也就是最新的 ...
- Python基础数据类型之集合
Python基础数据类型之集合 集合(set)是Python基本数据类型之一,它具有天生的去重能力,即集合中的元素不能重复.集合也是无序的,且集合中的元素必须是不可变类型. 一.如何创建一个集合 #1 ...
- TOJ 4008 The Leaf Eaters
|A∪B∪C|=|A|+|B|+|C|-|A∩B|-|A∩C|-|B∩C|+|A∩B∩C| 这个是集合的容斥,交集差集什么的,这个在概率论经常用到吧 4008: The Leaf Eaters T ...