初识Volley:JsonObjectRequest、StringRequest、RequestQueue
2016-09-26 16:14
519 查看
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);
Run一下,结果如下图所示,ok。
![](http://img.blog.csdn.net/20160926161124155)
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;
}
};
适用场景:
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) :
参数名 | 类型 | 必填 | 参数位置 | 描述 | 默认值 |
---|---|---|---|---|---|
apikey | string | 是 | header | API密钥 | 您自己的apikey |
参数名 | 类型 | 必填 | 参数位置 | 描述 | 默认值 |
---|---|---|---|---|---|
area | string | 是 | 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);
Run一下,结果如下图所示,ok。
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;
}
};
相关文章推荐
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest 或 JsonObjectRequest请求数据,中文显示乱码
- Android-Volley网络通信框架(StringRequest和JsonObjectRequset)
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
- Volley的Get、Post方式(JsonObjectRequest、StringRequest)以及Volley获取图片的3种方式
- Volley StringRequest和JSONObjectRequest的使用
- Volley StringRequest和JSONObjectRequest使用几个细节
- Android-Volley网络通信框架(StringRequest & JsonObjectRequest)
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley StringRequest和JSONObjectRequest使用几个细节
- Volley使用JsonObjectRequest发送Post请求失败
- Volley使用之JsonObjectRequest
- Volley使用JsonObjectRequest发送Post请求失败
- Volley框架网络访问String,JsonObject,JsonArray