【第一篇】Volley的使用之json请求

最近项目写完,有开始新的学习了,volley很久以前就接触了,也看了源码,然而却没有通过文章去记录自己的学习成果。

首先讲下volley的特点:

   1,扩展性强。Volley 中大多是基于接口的设计,可配置性强。

   2,一定程度符合 Http 规范,包括返回 ResponseCode(2xx、3xx、4xx、5xx)的处理,请求头的处理,缓存机制的支持等。并支持重试及优先级定义。

   3,默认 Android2.3 及以上基于 HttpURLConnection,2.3 以下基于 HttpClient 实现,这两者的区别及优劣在4.2.1 Volley中具体介绍。

   4,提供简便的图片加载工具。

现在计划是:
  1. 写volley相关demo
  2. 分析volley源码
  3. 扩展和完善volley,给volley添加一些更加方便开发的功能等。
在编写代码前首先导入volley的库或者引入jar包,这里不进行赘述,只讲使用。
demo1,json数据请求:
    首先请求的json数据格式:
  1. {
       "weatherinfo" : {
          "Radar" : "JC_RADAR_AZ9200_JB",
          "SD" : "77%",
          "WD" : "东南风",
          "WS" : "2级",
          "WSE" : "2",
          "city" : "广州",
          "cityid" : "101280101",
          "isRadar" : "1",
          "njd" : "暂无实况",
          "qy" : "1004",
          "temp" : "24",
          "time" : "10:45"
       }
    }
首先创建javaBean:
  1.   1 package com.soyoungboy.volleydemo.bean;
      2 import java.io.Serializable;
      3 import com.soyoungboy.volleydemo.utils.GsonImpl;
      4 public class WeathBean implements Serializable {
      5     /**
      6      * 
      7      */
      8     private static final long serialVersionUID = 1L;
      9     public Weatherinfo weatherinfo;
     10     @Override
     11     public String toString() {
     12         return "WeathBean [weatherinfo=" + weatherinfo + "]";
     13     }
     14     public  static WeathBean getBeanFromJson(String response){
     15         WeathBean weathBean= GsonImpl.get().toObject(response, WeathBean.class);
     16         return weathBean ;
     17     }
     18     /**
     19      * Temporary class name, create by Json2Class.
     20      */
     21     public static class Weatherinfo implements Serializable {
     22         public String SD;
     23         public int isRadar;
     24         public String time;
     25         public int WSE;
     26         public String WS;
     27         public String WD;
     28         public String njd;
     29         public int qy;
     30         public String Radar;
     31         public int temp;
     32         public int cityid;
     33         public String city;
     34         public String getSD() {
     35             return SD;
     36         }
     37         public void setSD(String sD) {
     38             SD = sD;
     39         }
     40         public int getIsRadar() {
     41             return isRadar;
     42         }
     43         public void setIsRadar(int isRadar) {
     44             this.isRadar = isRadar;
     45         }
     46         public String getTime() {
     47             return time;
     48         }
     49         public void setTime(String time) {
     50             this.time = time;
     51         }
     52         public int getWSE() {
     53             return WSE;
     54         }
     55         public void setWSE(int wSE) {
     56             WSE = wSE;
     57         }
     58         public String getWS() {
     59             return WS;
     60         }
     61         public void setWS(String wS) {
     62             WS = wS;
     63         }
     64         public String getWD() {
     65             return WD;
     66         }
     67         public void setWD(String wD) {
     68             WD = wD;
     69         }
     70         public String getNjd() {
     71             return njd;
     72         }
     73         public void setNjd(String njd) {
     74             this.njd = njd;
     75         }
     76         public int getQy() {
     77             return qy;
     78         }
     79         public void setQy(int qy) {
     80             this.qy = qy;
     81         }
     82         public String getRadar() {
     83             return Radar;
     84         }
     85         public void setRadar(String radar) {
     86             Radar = radar;
     87         }
     88         public int getTemp() {
     89             return temp;
     90         }
     91         public void setTemp(int temp) {
     92             this.temp = temp;
     93         }
     94         public int getCityid() {
     95             return cityid;
     96         }
     97         public void setCityid(int cityid) {
     98             this.cityid = cityid;
     99         }
    100         public String getCity() {
    101             return city;
    102         }
    103         public void setCity(String city) {
    104             this.city = city;
    105         }
    106         @Override
    107         public String toString() {
    108             return "Weatherinfo [SD=" + SD + ", isRadar=" + isRadar + ", time="
    109                     + time + ", WSE=" + WSE + ", WS=" + WS + ", WD=" + WD
    110                     + ", njd=" + njd + ", qy=" + qy + ", Radar=" + Radar
    111                     + ", temp=" + temp + ", cityid=" + cityid + ", city="
    112                     + city + "]";
    113         }
    114     }
    115 }

    创建界面:

activity_json.xml
  1. <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/lvWeatherTv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </TextView>
    </LinearLayout>

    主要的逻辑:

  1. 首先创建请求队列 mqueue;
  2. 创建json请求jsonObjectRequest,实现请求成功和失败的逻辑编写;
  3. 将请求放入请求队列中去。
 1 public class JsonActivity extends Activity {
 2     private static final String TAG = "com.soyoungboy.volleydemo.JsonActivity";
 3     private RequestQueue mQueue;
 4     private static final String WEATHER_LINK = "http://www.weather.com.cn/data/sk/101280101.html";
 5     private TextView lvWeatherTv;
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_json);
 9         lvWeatherTv = (TextView) findViewById(R.id.lvWeatherTv);
10         mQueue = Volley.newRequestQueue(this);
11         getWeatherInfo();
12     }
13     public void getWeatherInfo() {
14         JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
15                 WEATHER_LINK, null, new Response.Listener<JSONObject>() {
16                     @Override
17                     public void onResponse(JSONObject response) {
18                         if (!TextUtils.isEmpty(response.toString())) {
19                             WeathBean weathBean = WeathBean
20                                     .getBeanFromJson(response.toString());
21                             lvWeatherTv.setText(weathBean.toString());
22                         }
23                     }
24                 }, new Response.ErrorListener() {
25                     @Override
26                     public void onErrorResponse(VolleyError error) {
27                         ToastUtils.toastL(getApplicationContext(),
28                                 error.getMessage());
29                     }
30                 });
31         mQueue.add(jsonObjectRequest);
32     }
33 }
显示结果:
 
 
原文地址:https://www.cnblogs.com/androidsuperman/p/01c45a4cba5f15bdfed1e6f122d1d022.html