zoukankan      html  css  js  c++  java
  • android volley http请求框架

    2015年11月6日 14:35:19

    注意: 

    1.安卓官方的volley在google的codesource上, 在github上有他的镜像android-volley, 并解决了官方的一部分bug

    2.github上的镜像clone下来后, 用android studio打开(file->open...->dir/of/android-volley)时会自动构建, 并生成相应的jar和aar

    3.在自己的项目中使用volley.jar时, 不用再引入apache的httpclient或者httpcore (话外, httpclient 包含httpcore)

    测试代码:

     1 protected void onCreate(Bundle savedInstanceState)
     2     {
     3         super.onCreate(savedInstanceState);
     4         setContentView(R.layout.activity_main);
     5 
     6         String url = "http://www.zhangzhibin.com/test/index/androidtest";
     7         RequestQueue mqueue = Volley.newRequestQueue(this);
     8 
     9         StringRequest strRequest = new StringRequest(
    10                 url,
    11                 new Response.Listener<String>()
    12                 {
    13                     @Override
    14                     public void onResponse(String response)
    15                     {
    16                         Log.d("TAG", response);
    17                     }
    18                 },
    19 
    20                 new Response.ErrorListener()
    21                 {
    22                     @Override
    23                     public void onErrorResponse(VolleyError error)
    24                     {
    25                         Log.d("TAG", "onErrorResponse "+error.getMessage(), error);
    26                     }
    27 
    28                 }
    29         );
    30 
    31         mqueue.add(strRequest);
    32 
    33         JsonArrayRequest jsonArrRequest = new JsonArrayRequest(
    34                 url,
    35                 new Response.Listener<JSONArray>()
    36                 {
    37                     public void onResponse(JSONArray response)
    38                     {
    39                         Log.d("TAG", response.toString());
    40                     }
    41                 },
    42                 new Response.ErrorListener()
    43                 {
    44                     public void onErrorResponse (VolleyError error)
    45                     {
    46                         Log.d("TAG", "volley error ==> "+error.getMessage(), error);
    47                     }
    48                 }
    49         );
    50 
    51         mqueue.add(jsonArrRequest);
    52 
    53         JsonObjectRequest jsonObjRequest = new JsonObjectRequest(
    54                 url,
    55                 new Response.Listener<JSONObject>()
    56                 {
    57                     public void onResponse(JSONObject response)
    58                     {
    59                         Log.d("TAG", response.toString());
    60                     }
    61                 },
    62                 new Response.ErrorListener()
    63                 {
    64                     public void onErrorResponse (VolleyError error)
    65                     {
    66                         Log.d("TAG", "volley error ==> "+error.getMessage(), error);
    67                     }
    68                 }
    69         );
    70 
    71         mqueue.add(jsonObjRequest);
    72     }

    参考: 

    使用中文简介: http://www.kwstu.com/ArticleView/kwstu_20144118313429

    github: https://github.com/mcxiaoke/android-volley

    jar/aar: http://blog.csdn.net/qiujuer/article/details/39754517

  • 相关阅读:
    Solr 删除数据的几种方式
    velocity 随笔
    LOG4J.PROPERTIES配置详解(转载)
    转 如何使用velocity模板引擎开发网站
    通过pinyin4j将汉字转换为全拼 和 拼音首字母
    去除数组中的重复数据
    java 转义字符
    多重背包(学习笔记)
    Team Queue
    [HAOI2008]糖果传递
  • 原文地址:https://www.cnblogs.com/iLoveMyD/p/4942544.html
Copyright © 2011-2022 走看看