听说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. js匿名函数使用&传参

    function () { alert("error"); } //报错:匿名函数不能直接调用 使用 0x01 (function () { alert("Success ...

  2. learn OpenStack by picture

  3. Qt之QSS(Q_PROPERTY-自定义属性)

    版权声明:进步始于交流,收获源于分享!纯正开源之美,有趣.好玩.靠谱...作者:一去丶二三里 博客地址:http://blog.csdn.net/liang19890820   目录(?)[+]   ...

  4. supervisor运行virtualenv环境下的nagios-api

    supervisord-example.conf [unix_http_server] file=/tmp/supervisor.sock ; path to your socket file [su ...

  5. Python类三种方法,函数传参,类与实例变量(一)

    1 Python的函数传递: 首先所有的变量都可以理解为内存中一个对象的'引用' a = 1 def func(a): a = 2 func(a) print(a) # 1 a = 1 def fun ...

  6. poj2253 Frogger dijkstra

    题目大意: 给出n个岛的坐标,前两个坐标分别为A青蛙和B青蛙所在岛的坐标,A青蛙想到达B青蛙所在的岛,A可以从某一个岛跳到任意其它一个岛上,则A到B的每条路径都有一个跳的最远的距离Xi,求这些最远距离 ...

  7. 因子和&&因子数

    给定一数n,求n的因子数目和因子之和 上述求因子和式子等于 ∏{(piei+1 - 1) / (pi - 1)}  (∏就是连乘) 模板: +; int prime[maxn]; bool is_pr ...

  8. lua 语句学习

    就如同C里的if else,while,do,repeat.就看lua里怎么用: 1.首先看if else t = {1,2,3} local i = 1 if t[i] and t[i] % 2 = ...

  9. python:进程操作

    一.多进程应用 import socket from multiprocessing import Process def talk(conn): conn.send(b'connected') re ...

  10. [APIO/CTSC 2007]数据备份

    嘟嘟嘟 这竟然是一道贪心题,然而我在不看题解之前一直以为是dp. 首先最优的配对一定是相邻两个建筑物配对,所以我们求出差分数组,就变成了在n - 1个数中选出不相邻的k个数,使这k个数的和最小. 贪心 ...