- 1、本文档共10页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
Android网络开发库Volley使用教程讲述
Android网络开发库Volley使用教程
Volley是Google推出的一个网络请求库,已经被放到了Android源码中,地址在这里,先看使用方法
RequestQueue mRequestQueue = Volley.newRequestQueue(context);
JsonObjectRequest req = new JsonObjectRequest(URL, null,
new Response.ListenerJSONObject() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v(Response:%n %s, response.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e(Error: , error.getMessage());
}
});
mRequestQueue.add(req);
详细的使用方法就不说了,网上很多,可以看下这个,这里只大概介绍一下Volley的工作方法,就从上面的例子开始。
我们接触到的Volley的核心就两个,从名字就可以看出其用途。
RequestQueue
Request
前面我们看到RequestQueue是通过Volley的方法newRequestQueue获得的,Volley类的唯一作用就是获取RequestQueue的实例,而我们完全可以自己new RequestQueue,不知道为什么不把这两个类合并了。
/** * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
*
* @param context A {@link Context} to use for creating the cache dir.
* @param stack An {@link HttpStack} to use for the network, or null for default.
* @return A started {@link RequestQueue} instance.
*/ public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = volley/0;
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + / + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT = 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: /2011/09/androids-http-clients.html
文档评论(0)