初识Volley:JsonObjectRequest、StringRequest、RequestQueue
来源:互联网 发布:unity3d 卡通场景 编辑:程序博客网 时间:2024/04/28 20:50
Volley的中文翻译为“齐射、并发”,是在2013年的Google大会上发布的一款Android平台网络通信库,具有网络请求的处理、小图片的异步加载和缓存等功能,能够帮助 Android APP 更方便地执行网络操作,而且更快速高效。
适用场景:
Volley:数据量小,网络通信频繁的操作,即时通信、社交等应用;
XUtils:大文件上传与下载等以及各种工具类的使用,电商、社区等应用。
不同场景不同工具。
话不多说,开始项目,因为我们网络请求需要访问API,因此在这里我们先介绍:
百度的APIStore:
http://apistore.baidu.com/
为开发者提供最全面的API服务。
在这里我们用到天气预报_易源。(http://apistore.baidu.com/apiworks/servicedetail/515.html)
接口地址 :http://apis.baidu.com/showapi_open_bus/weather_showapi/areaid
请求方法 :GET
请求参数(header) :
请求参数(urlParam) :
开始Coding,先写一个布局文件,这里写一个TextView用于显示请求API后返回的数据:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.quan.car.volleytest.MainActivity"> <TextView android:id="@+id/message_tv_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /></RelativeLayout>
因为涉及到网络的访问,我们还需要添加相应权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
ok,开始加载Volley框架。如果你在AS中,那么太简单了,在相应Module的build.gradle的dependencies中添加一行代码:
compile 'com.mcxiaoke.volley:library:1.0.+'然后同步一下,就可以加载Volley框架了。
准备工作搞定,下面开始核心源代码:
1.准备控件和自己的APIKey(注册账号,申请十分方便):
private TextView message_tv_main; public static String API_KEY = "xxx";
2.初始化控件和RequestQueue(管理Request)
message_tv_main = (TextView) findViewById(R.id.message_tv_main); RequestQueue requestQueue = Volley.newRequestQueue(this);
3.参考API定义url、param、Header,重写Request相应方法。
String urlJsonObjectRequest = "http://apis.baidu.com/showapi_open_bus/weather_showapi/areaid?area=%E5%8C%97%E4%BA%AC"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(urlJsonObjectRequest, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { message_tv_main.setText(response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { message_tv_main.setText(error.getMessage()); } }) { @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("apikey", API_KEY); return hashMap; } };
4.将request添加到RequestQueue中。
requestQueue.add(jsonObjectRequest);
StringRequest使用方法类似:
String url = "http://apis.baidu.com/apistore/idservice/id"; String param = "id=420984198704207896"; String urlParam = url + "?" + param; StringRequest stringRequest = new StringRequest(urlParam, new Response.Listener<String>() { @Override public void onResponse(String response) { message_tv_main.setText(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { message_tv_main.setText(error.getMessage()); } }){ @Override public Map<String, String> getHeaders() throws AuthFailureError { HashMap<String,String> hashMap = new HashMap<>(); hashMap.put("apikey",API_KEY); return hashMap; } };
0 0
- 初识Volley:JsonObjectRequest、StringRequest、RequestQueue
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest的使用
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Android Volley框架(一):StringRequest、JsonObjectRequest、JsonArrayRequest
- Android-Volley网络通信框架(StringRequest & JsonObjectRequest)
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
- Volley RequestQueue
- volley JsonObjectRequest 提交参数
- Volley使用之JsonObjectRequest
- Volley之RequestQueue
- tomcat部署三种方法
- Java对象池技术的原理及其实现
- git pull
- 我理解的数字证书-1-公钥,私钥和数字证书
- shadowsocks下载地址
- 初识Volley:JsonObjectRequest、StringRequest、RequestQueue
- 【Cherry云计算】云计算特征
- bootstrap轮播图不能自动播放
- 在gitignore中设置不忽略的文件(夹)
- 刷题神器,软考助手V1.4上线啦
- 社区发现算法——标签传播算法LPA
- 使用驱动器中的光盘之前需要将其格式化怎么弄
- JVM类加载
- 机械零点、MAM 文件 、 EMT标定原理