Android-Volley网络通信框架(StringRequest & JsonObjectRequest)
1.回想
上篇对 Volley进行了简介和对它的学习目的与目标,最后,为学习Volley做了一些准备
2.重点
2.1 RequestQueue 请求队列的建立
2.2 学习 StringRequest和JsonObjectRequest 。
3.RequestQueue 请求队列的建立
新建类 volleyApplication 继承自Application , 使用单例模式创建 请求处理队列, 实现例如以下:
package com.example.volleyHttp; import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley; import android.app.Application; public class volleyApplication extends Application { /**
* 01. 建立 请求队列
* 02. 将 请求队列 增加到 AndroidMain.xml中
* 03.
*/ private static RequestQueue queue; @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
queue=Volley.newRequestQueue(getApplicationContext());
} //入口
public static RequestQueue getQueue(){
return queue;
} }
这个能够与Activity 发生联动作用。在Activity 退出的时候,能够取消的 网络请求操作(通过 tag实现);以下能够实现:
@Override
protected void onStop() {
// TODO Auto-generated method stub
volleyApplication.getQueue().cancelAll("strReqGet");
super.onStop();
}
使用的时候须要在 AndroidMainfist.xml 文件中 的 Application 标签下 注冊 刚才的 volleylication:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
<span style="color:#ff0000;"> android:name="com.example.volleyHttp.volleyApplication"</span>
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
不要忘记了加入网络权限!
4.StringRequest 的get和post方法
4.1 get 操作
(1)实例化StringRequest 对象
(2)设置參数:请求方式,URL地址,成功的返回调用。失败的返回调用。
(3)给请求设置 tag。加入到刚才的请求队列 中!
private void strRequest_get() {
StringRequest request = new StringRequest(Method.GET,
HttpPath.getSharedIfo(1), new Listener<String>() { @Override
public void onResponse(String response) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), response,
Toast.LENGTH_SHORT).show();
tv.setText(response);
}
}, new Response.ErrorListener() { @Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
}); request.setTag("strReqGet");
<span style="color:#ff0000;">volleyApplication.getQueue().add(request);</span>
}
4.2 post 操作
(1)实例化StringRequest 对象
(2)设置參数:请求方式,URL地址,成功的返回调用,失败的返回调用;
(3)Post提交的參数设置:重写 getParams 方法。返回Map集合,将自己主动调用;
(4)请求设置 tag,加入到刚才的请求队列 中!
/**
* 01.2 String Requset post 提交数据
*/
private void strRequest_post(){
StringRequest stringRequest=new StringRequest(Method.POST,HttpPath.getSharedIfo_post(),new Listener<String>() { @Override
public void onResponse(String response) {
// 成功返回数据
tv.setText(response);
}
},new Response.ErrorListener() { @Override
public void onErrorResponse(VolleyError error) {
//出错
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
// post 提交 重写參数 ,将自己主动 提交參数
Map<String,String> map=new HashMap<String, String>();
map.put("id","2");
return map;
}
}; stringRequest.setTag("strPost");
volleyApplication.getQueue().add(stringRequest); }
5.JsonObjectRequest 的 get和post方法
5.1 get 方法
(1)实例化JsonObjectRequest 对象
(2)设置參数:请求方式,URL地址。參数Jsonrequest 为 null (由于为get请求),成功的返回调用。失败的返回调用;
(3)给请求设置 tag,加入到刚才的请求队列 中。
(4)请求成功后,直接返回 成 JsonObject 对象 。能够直接使用
/**
* 02.jsonobjectRequert get请求
*/
private void jsonRequest_get(){
JsonObjectRequest objectRequest=new JsonObjectRequest(Method.GET,HttpPath.getSharedIfo(1),
null,new Listener<JSONObject>() { @Override
public void onResponse(JSONObject response) {
//直接进行 json解析
try {
JSONObject jsonObject=new JSONObject(response.getString("data"));
tv.setText(jsonObject.getString("note"));
} catch (JSONException e) {
// TODO Auto-generated catch block
tv.setText(e.getMessage());
}
}
},new Response.ErrorListener() { @Override
public void onErrorResponse(VolleyError error) {
//请求失败返回的信息
tv.setText(error.getMessage());
}
});
objectRequest.setTag("jsonRequest");
volleyApplication.getQueue().add(objectRequest);
}
5.2 post 方法
(1)实例化JsonObjectRequest 对象
(2)设置參数:请求方式,URL地址,參数JsonRequest ,成功的返回调用,失败的返回调用。
(3)请求參数设置:通过 JsonObejct 实现 post提交 參数设置 (见演示样例代码)
(4)给请求设置 tag。加入到刚才的请求队列 中。
(5)请求成功后。直接返回 成 JsonObject 对象 ,能够直接使用
/**
* 02.2 jsonObjectRequest post的方式 请求
*/
private void jsonRequest_post(){ <span style="color:#ff0000;"> //封装请求參数 </span>
JSONObject jsonStr=new JSONObject();
try {
jsonStr.put("id","2");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Method.POST,HttpPath.getSharedIfo_post()
,jsonStr, new Listener<JSONObject>() { @Override
public void onResponse(JSONObject response) {
// TODO Auto-generated method stub
JSONObject jsonObject;
try {
jsonObject = new JSONObject(response.getString("data"));
tv.setText(jsonObject.getString("note"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
},new Response.ErrorListener() { @Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
tv.setText(error.getMessage());
}
});
jsonObjectRequest.setTag("jsonPost");
volleyApplication.getQueue().add(jsonObjectRequest); }
6.JsonArrayRequest
这个我就不在累赘了,和 JsonObjectResquest 一样 , 仅仅只是返回的是 JsonArray 类型。
7. 注意
RequestQueue 请求队列 在初始化的时候,一定要在 android 配置文件的Application 标签里进行注冊。
使用的时候。注意 导入 包问题,ErrorListener 一定是 Response.ErrorListener;
Android-Volley网络通信框架(StringRequest & JsonObjectRequest)的更多相关文章
- Android开发之Volley网络通信框架
今天用了一下Volley网络通信框架,感觉挺好用的,写个博客记录一下用法.方便以后VC. Volley(Google提供的网络通信库,能使网络通信更快,更简单,更健壮.) 功能模块: 1. JSON, ...
- Android应用框架-Volley网络通信框架
1.Volley简介: Volley是Google 推出的 Android 异步网络请求框架和图片加载框架. 在 Google I/O 2013 大会上发布. 2.Volley特点 扩展性强. And ...
- Volley网络通信框架
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- Android-Volley网络通信框架(自己定义Request 请求:实现 GsonRequest)
1.回想 上篇学习了android 通过 volley 网络通信框架 实现 请求图片的三种方法! 2.重点 (1)复习和熟悉 StringRequest ,JsonObjectRequest 方法 ( ...
- Android-Volley网络通信框架(二次封装数据请求和图片请求(包含处理请求队列和图片缓存))
1.回想 上篇 使用 Volley 的 JsonObjectRequest 和 ImageLoader 写了 电影列表的样例 2.重点 (1)封装Volley 内部 请求 类(请求队列,数据请求,图片 ...
- Android 网络通信框架Volley简介(Google IO 2013)
1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient( ...
- [转]Android 网络通信框架Volley简介(Google IO 2013)
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- Android 网络通信框架Volley基本介绍
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- 【转】Android 网络通信框架Volley简介(Google IO 2013)
Volley主页 https://android.googlesource.com/platform/frameworks/volley http://www.youtube.com/watch?v= ...
- Android 网络通信框架Volley(一)
转自:http://blog.csdn.net/t12x3456/article/details/9221611 1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫 ...
随机推荐
- class-metaclass-Class vs. type
In some languages, classes are only a compile-time feature (new classes cannot be declared at runtim ...
- idea运行提示Error:java:无效的源发行版:1.9
如果你是jdk1.8 改到8即可,如图:
- [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂
Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...
- 解决new Date的值为Invalid Date、NaN-NaN的问题
错误代码: let timespan = 1515239514230; let dateTime = new Date(timespan); console.log(dateTime) // 返回 I ...
- 将页面的内容导出使用html2canvas+jsPDF
第一首先是要引用 import jsPDF from 'jspdf' import html2canvas from 'html2canvas' import PDFJS from 'pdfjs-di ...
- android开发游记:ItemTouchHelper 使用RecyclerView打造可拖拽的GridView
以下是RecyclerView结合ItemTouchHelper实现的列表和网格布局的拖拽效果. 效果图例如以下:(gif图有点顿卡,事实上执行是非常流畅的) demo下载地址: DragRecycl ...
- hdu_1166,线段树单点更新
在刷线段树,参考自http://www.notonlysuccess.com/index.php/segment-tree-complete/ #include<iostream> #in ...
- 基于深度学习的病毒检测技术无需沙箱环境,直接将样本文件转换为二维图片,进而应用改造后的卷积神经网络 Inception V4 进行训练和检测
话题 3: 基于深度学习的二进制恶意样本检测 分享主题:全球正在经历一场由科技驱动的数字化转型,传统技术已经不能适应病毒数量飞速增长的发展态势.而基于沙箱的检测方案无法满足 APT 攻击的检测需求,也 ...
- [C++] upper_bound和lower_bound
upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...
- Hive框架基础(一)
* Hive框架基础(一) 一句话:学习Hive有毛用? 那么解释一下 毛用: * 操作接口采用类SQL语法,提供快速开发的能力(不会Java也可以玩运算) * 避免了去写MapReduce,减少开发 ...