听说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的使用的更多相关文章

  1. Android Volley和Gson实现网络数据加载

    Android Volley和Gson实现网络数据加载 先看接口 1 升级接口 http://s.meibeike.com/mcloud/ota/cloudService POST请求 参数列表如下 ...

  2. [Android]Volley的使用

    Volley是Google I/O 2013上提出来的为Android提供简单快速网络访问的项目.Volley特别适合数据量不大但是通信频繁的场景. 优势 相比其他网络载入类库,Volley 的优势官 ...

  3. Android Volley入门到精通:定制自己的Request

    经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...

  4. Android Volley完全解析

    1. Volley简介 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行H ...

  5. Android Volley框架的使用(二)

     此博文源码下载地址  https://github.com/Javen205/VolleyDemo.git 使用请求队列RequestQueue Volley中的Request都需要添加到Reque ...

  6. Android Volley 之自定义Request

    转载标明出处:http://blog.csdn.net/lmj623565791/article/details/24589837 今天群里一哥们需要自定义Volley的Request的例子,于是产生 ...

  7. Android Volley 之自己定义Request

    转载标明出处:http://blog.csdn.net/lmj623565791/article/details/24589837 今天群里一哥们须要自己定义Volley的Request的样例,于是产 ...

  8. Android Volley彻底解决(三),定制自己Request

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17612763 经过前面两篇文章的学习,我们已经掌握了Volley各种Request ...

  9. Android Volley完全解析(三),定制自己的Request

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/17612763 经过前面两篇文章的学习,我们已经掌握了Volley各种Request ...

随机推荐

  1. Avro-RPC client in Flume

    Avro used in Flume Define the interface of RpcClient public interface RpcClient { public int getBatc ...

  2. Easy deployment

    Use simple ssh and shell scripts to deploy, upgrade, rollback and reconfigure linux servers. https:/ ...

  3. log4j2简单使用

    http://www.cnblogs.com/lzb1096101803/p/5796849.html 如果是基于的slf4j,看这篇文章去配置 http://blog.csdn.net/neosmi ...

  4. 玩得一手好注入之order by排序篇

    看了之前Gr36_前辈在先知上的议题,其中有提到排序注入,这个在最近经常遇到这样的问题,所以先总结下order by 排序注入的知识. 0×00 背景 看了之前Gr36_前辈在先知上的议题,其中有提到 ...

  5. 初次撸Python,踩平些小坑~

    [转义字符] os.path.isdir(targetPath)总是报错,以为字符串不行而是要转成file类型,尝试了很多方法还是没解决,最后发现,windows下的路径中字符串中需要转义(例子在网上 ...

  6. HttpClient拉取连载小说

    上午刚入手的小说,下午心血来潮想从网站上拉取下来做成电子书,呵呵,瞎折腾-说做就做- [抓包] 这一步比什么都重要,如果找不到获取真正资源的那个请求,就什么都不用做了- 先是打算用迅雷把所有页面都下载 ...

  7. Struts2学习-拦截器2

    1.做一个登陆页面(loginView.jsp,才用Action来访问),2.登陆成功后,可以跳转到系统的首页(index.jsp),3.首页有一个链接(testOtherAction访问其它的功能模 ...

  8. C#转Java之路之二:多线程原子变量

    多线程操作会带来不一致性,为了实现一直性.我们可以用关键字:synchronized同步对象或者volatile轻量级.内存可见性. 两个关键字使用对比: 1.synchronized比较重,属于悲观 ...

  9. ZOJ 2386 容斥原理

    题意:给出n个数,和m(1<=m<=200 000 000),求1~M中能被这n个数其中任意一个数整除的个数: 分析:n范围很小,可以枚举选择被哪些数整除,被奇数个整数整除加m/这个n个数 ...

  10. public /protected/private的作用域

    作用域 当前类 同一package 子孙类 其他package public √ √ √ √ protected √ √ √ × friendly √ √ × × private √ × × ×