android Volley+Gson的使用
听说Volley框架非常好用,今天试了一下post请求,果然如此,因为我传的是json获取的也是json所以就写了一种关于json的请求,其实其他的代码都差不多.首先要先创建一个全局的变量,请求入队列使用代码如下:
public class MyApplication extends Application{
public static RequestQueue requestQueue;
//public static Context context;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
requestQueue=Volley.newRequestQueue(getApplicationContext());
}
public static RequestQueue getHttpRequestQueue(){
return requestQueue;
}
}
然后开始请求代码如下:
public class Volley {
public String url="http://api.avatardata.cn/Joke/NewstJoke";
String resultStr;
Gson gson;
public Volley(){
volleyPost();
}
public void volleyPost(){
StringRequest request=new StringRequest(Method.POST,url,new Listener<String>() {
@Override
public void onResponse(String response) {
// TODO Auto-generated method stub
//System.out.println(response.toString());
resultStr=response.toString();
Gson gson=new Gson();
Idiom idiom=gson.fromJson(resultStr, Idiom.class);
System.out.println(idiom);
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
// System.out.println(error.toString());
resultStr=error.toString();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
// TODO Auto-generated method stub
Map<String,String> map=new HashMap<String, String>();
map.put("key", "yourkey");
map.put("dtype", "JSON");
map.put("format", "true");
map.put("page", "1");
map.put("rows", "10");
return map;
}
};
MyApplication.getHttpRequestQueue().add(request);
}
}
这样就可以轻松获取json数据。另外获取数据如何解析呢? 简单的json我们直接用android 里面的jsonObject就可以解决,但是稍微复杂一点,包含数组什么的那样的话会很麻烦,不要紧,可以用gson来进行json解析,就是把获取json串的字段全部封装到对象里面,遇到[ ]就用List,遇到{}就用对象来定义,一层层的封装,然后实例化gson,就可以很容易的获取到数据。
如下字符串,截取不完整:
好吧,就这样吧,需要代码的话可以找我。因为我比较懒,所以大家将就点吧,虽然不能看到详细的代码,但是方法还是很清楚的了解的。
一下是我封装的两个类
android Volley+Gson的使用的更多相关文章
- Android Volley和Gson实现网络数据加载
Android Volley和Gson实现网络数据加载 先看接口 1 升级接口 http://s.meibeike.com/mcloud/ota/cloudService POST请求 参数列表如下 ...
- [Android]Volley的使用
Volley是Google I/O 2013上提出来的为Android提供简单快速网络访问的项目.Volley特别适合数据量不大但是通信频繁的场景. 优势 相比其他网络载入类库,Volley 的优势官 ...
- Android Volley入门到精通:定制自己的Request
经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...
- Android Volley完全解析
1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...
- Android Volley框架的使用(二)
此博文源码下载地址 https://github.com/Javen205/VolleyDemo.git 使用请求队列RequestQueue Volley中的Request都需要添加到Reque ...
- Android Volley 之自定义Request
转载标明出处:http://blog.csdn.net/lmj623565791/article/details/24589837 今天群里一哥们需要自定义Volley的Request的例子,于是产生 ...
- Android Volley 之自己定义Request
转载标明出处:http://blog.csdn.net/lmj623565791/article/details/24589837 今天群里一哥们须要自己定义Volley的Request的样例,于是产 ...
- Android Volley彻底解决(三),定制自己Request
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17612763 经过前面两篇文章的学习,我们已经掌握了Volley各种Request ...
- Android Volley完全解析(三),定制自己的Request
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17612763 经过前面两篇文章的学习,我们已经掌握了Volley各种Request ...
随机推荐
- 前端(三大框架、Bootstrap,jQuery,自整理)
前端,HTML(超文本标记语言),CSS(层叠样式表)和JavaScript(脚本语言) HTML,通常说的h5,其实按标准来说,HTML4的后续版本不带编号了,并保证向前的兼容性 CSS的版本3,增 ...
- 【Linux】Windows与Linux之间的文件共享(基于网络)
切记:Linux的安全机制的存在--iptables和selinux. 一.操作前提 1.1 从Windows能够ping通Linux 1.2 关闭Linux防火墙 command1:/etc/ini ...
- qt 创建线程
http://www.cnblogs.com/xinxue/p/6840315.html Qt 之 QtConcurrent 本文以 Qt 中的 QtConcurrent::run() 函数为例,介绍 ...
- 多线程(五)~ wait/notify机制(等待/通知)
首先我们来看一张图,这张图描述了线程操作的几个步骤. 图已经描述的很清楚了,这里除了wait()之外,其他的前面都已经接触过了. 这一章我们主要来说一下和wait()相关的操作,其实和wait()相关 ...
- Servlet和SpringMVC补课
1.web.xml加载顺序 http://mianhuaman.iteye.com/blog/1105522 关键点:ServletContext -> context-param -> ...
- 推荐一个很好用的脚本session snapper
源网址http://tech.e2sn.com/oracle-scripts-and-tools/session-snapper 内容: If you want to just download Sn ...
- March 15 2017 Week 11 Wednesday
The starting point of all achievements is desire. 成功的第一步是渴望. Only you desire for somethings, you can ...
- ABAP的语法高亮是如何在浏览器里显示的
这篇文章的原文我发表在SAP官方社区上:https://blogs.sap.com/2018/03/09/how-abap-syntax-highlight-is-implemented-in-web ...
- python26 re正则表达式
#coding:utf-8 #/usr/bin/python """ 2018-11-25 dinghanhua re """ import ...
- angular2 里父子组件传值的坑
1.如果传的是基本类型的值,子组件里改变该值,父组件无变化 2.如果传的是对象,子组件里改变对象里的变量,父组件会变化 3.如果传的是函数,this不会继续指向父组件对象了,如果需要this指向父组件 ...