Android学习笔记:Andorid网络请求框架Volley的使用(上)
2015-08-25 15:53
701 查看
Volley框架是Google I/O 2013大会上发布的。Volley是Google针对Android平台上的网络通信库而诞生,该框架能使网络通信更快,更简单,更健壮。Volley的特点有:
Ⅰ:通信更简单更快捷
ll:Get,Post网络请求及网络图像进行高效异步处理
III:可以对多个网络请求进行优先级排序以级多级别取消操作
IV:网络请求缓存及与Activity生命周期进行联动,在Activity销毁的时候可以对网络请求进行取消操作,防止内存泄露。
Volley的使用很简单:
1,下载Volley的jar包或是源代码,github地址:https://github.com/stormzhang/AndroidVolley
2,获取RequestQueue,一般我们是放到Application中进行初始化操作
3,实例化一个Request对象(StringRequest,JsonObjectRequest,JsonArrayRequest)
4,将Request加入RequestQueue即可。
首先学习的是最基础的StringRequest:
主页面布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp" >
<Button
android:id="@+id/get_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GET请求"
android:textSize="16sp"
android:layout_marginTop="15dp"/>
<Button
android:id="@+id/post_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="POST请求"
android:textSize="16sp" />
</LinearLayout>
-------------请求操作------------------------
public class MainActivity extends Activity implements OnClickListener {
private Button getBtn, postBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBtn = (Button) findViewById(R.id.get_btn);
postBtn = (Button) findViewById(R.id.post_btn);
getBtn.setOnClickListener(this);
postBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_btn:// GET请求获取数据
getStringRequest();
break;
case R.id.post_btn:// POST请求获取数据
postStringequest();
break;
}
}
private void getStringRequest() {// 用GET方法使用字符串数据请求StringRequest
String url = "http://apis.juhe.cn/mobile/get?phone=134****4320&key=d6099881640aed5e0bacc058cfd4357b";//key值是在聚合数据上申请的api
StringRequest strRequest = new StringRequest(Method.GET, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {// 请求成功
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {// 请求失败
Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();
}
});
strRequest.setTag("getLDM");
MyApplication.getRequestQueues().add(strRequest);
}
private void postStringequest() {// 用POST方法使用字符串数据请求StringRequest
String url = "http://apis.juhe.cn/mobile/get?";
StringRequest postRequest = new StringRequest(Method.POST, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {// getparams方法是Volley中使用POST请求数据时传递参数信息
Map<String, String> map = new HashMap<String, String>();
map.put("phone", "134****4320");
map.put("key", "d6099881640aed5e0bacc058cfd4357b");
return map;
}
};
postRequest.setTag("postLDM");
MyApplication.getRequestQueues().add(postRequest);
}
}
Ⅰ:通信更简单更快捷
ll:Get,Post网络请求及网络图像进行高效异步处理
III:可以对多个网络请求进行优先级排序以级多级别取消操作
IV:网络请求缓存及与Activity生命周期进行联动,在Activity销毁的时候可以对网络请求进行取消操作,防止内存泄露。
Volley的使用很简单:
1,下载Volley的jar包或是源代码,github地址:https://github.com/stormzhang/AndroidVolley
2,获取RequestQueue,一般我们是放到Application中进行初始化操作
3,实例化一个Request对象(StringRequest,JsonObjectRequest,JsonArrayRequest)
4,将Request加入RequestQueue即可。
首先学习的是最基础的StringRequest:
主页面布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp" >
<Button
android:id="@+id/get_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="GET请求"
android:textSize="16sp"
android:layout_marginTop="15dp"/>
<Button
android:id="@+id/post_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="POST请求"
android:textSize="16sp" />
</LinearLayout>
-------------请求操作------------------------
public class MainActivity extends Activity implements OnClickListener {
private Button getBtn, postBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getBtn = (Button) findViewById(R.id.get_btn);
postBtn = (Button) findViewById(R.id.post_btn);
getBtn.setOnClickListener(this);
postBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.get_btn:// GET请求获取数据
getStringRequest();
break;
case R.id.post_btn:// POST请求获取数据
postStringequest();
break;
}
}
private void getStringRequest() {// 用GET方法使用字符串数据请求StringRequest
String url = "http://apis.juhe.cn/mobile/get?phone=134****4320&key=d6099881640aed5e0bacc058cfd4357b";//key值是在聚合数据上申请的api
StringRequest strRequest = new StringRequest(Method.GET, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {// 请求成功
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {// 请求失败
Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();
}
});
strRequest.setTag("getLDM");
MyApplication.getRequestQueues().add(strRequest);
}
private void postStringequest() {// 用POST方法使用字符串数据请求StringRequest
String url = "http://apis.juhe.cn/mobile/get?";
StringRequest postRequest = new StringRequest(Method.POST, url, new Listener<String>() {
@Override
public void onResponse(String arg0) {
Toast.makeText(MainActivity.this, arg0, Toast.LENGTH_LONG).show();
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(MainActivity.this, arg0.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {// getparams方法是Volley中使用POST请求数据时传递参数信息
Map<String, String> map = new HashMap<String, String>();
map.put("phone", "134****4320");
map.put("key", "d6099881640aed5e0bacc058cfd4357b");
return map;
}
};
postRequest.setTag("postLDM");
MyApplication.getRequestQueues().add(postRequest);
}
}
相关文章推荐
- 计算机网络组网与配置技术—— 读书笔记3、传输介质和组网连接方法
- 《HTTP权威指南》阅读笔记(六)
- caffe siamese 网络 对比损失函数
- 网络安全技术(二)
- This absolute uri http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
- linux网络编程5:gethostbyname&&向固定地址发起请求得到返回页面字符
- 计算机网络组网与配置技术—— 读书笔记2、计算机网络基本技术
- 计算机网络组网与配置技术—— 读书笔记1、组网与配置基础知识
- HTTP&nbsp;请求已超过分配的超时。为此…
- http2.0
- httpClient访问http的rest接口
- LINUX2.4.x网络安全框架
- HTTP返回代码 201 304 404 500等代表的含义
- 解析 HTTP(HttpURLConnection getResponseCode)
- application/x-www-form-urlencoded等字符编码的解释说明(http)
- HttpServletRequest接口实例化的使用
- VMware网络连接失败
- 《HTTP权威指南》阅读笔记(五)
- 交换机用光纤收发器作聚合引起的网络故障
- REST,HTTP POST demo