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

时间:2024-06-11 14:07:02

最近项目写完,有开始新的学习了,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.  package com.soyoungboy.volleydemo.bean;
    import java.io.Serializable;
    import com.soyoungboy.volleydemo.utils.GsonImpl;
    public class WeathBean implements Serializable {
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    public Weatherinfo weatherinfo;
    @Override
    public String toString() {
    return "WeathBean [weatherinfo=" + weatherinfo + "]";
    }
    public static WeathBean getBeanFromJson(String response){
    WeathBean weathBean= GsonImpl.get().toObject(response, WeathBean.class);
    return weathBean ;
    }
    /**
    * Temporary class name, create by Json2Class.
    */
    public static class Weatherinfo implements Serializable {
    public String SD;
    public int isRadar;
    public String time;
    public int WSE;
    public String WS;
    public String WD;
    public String njd;
    public int qy;
    public String Radar;
    public int temp;
    public int cityid;
    public String city;
    public String getSD() {
    return SD;
    }
    public void setSD(String sD) {
    SD = sD;
    }
    public int getIsRadar() {
    return isRadar;
    }
    public void setIsRadar(int isRadar) {
    this.isRadar = isRadar;
    }
    public String getTime() {
    return time;
    }
    public void setTime(String time) {
    this.time = time;
    }
    public int getWSE() {
    return WSE;
    }
    public void setWSE(int wSE) {
    WSE = wSE;
    }
    public String getWS() {
    return WS;
    }
    public void setWS(String wS) {
    WS = wS;
    }
    public String getWD() {
    return WD;
    }
    public void setWD(String wD) {
    WD = wD;
    }
    public String getNjd() {
    return njd;
    }
    public void setNjd(String njd) {
    this.njd = njd;
    }
    public int getQy() {
    return qy;
    }
    public void setQy(int qy) {
    this.qy = qy;
    }
    public String getRadar() {
    return Radar;
    }
    public void setRadar(String radar) {
    Radar = radar;
    }
    public int getTemp() {
    return temp;
    }
    public void setTemp(int temp) {
    this.temp = temp;
    }
    public int getCityid() {
    return cityid;
    }
    public void setCityid(int cityid) {
    this.cityid = cityid;
    }
    public String getCity() {
    return city;
    }
    public void setCity(String city) {
    this.city = city;
    }
    @Override
    public String toString() {
    return "Weatherinfo [SD=" + SD + ", isRadar=" + isRadar + ", time="
    + time + ", WSE=" + WSE + ", WS=" + WS + ", WD=" + WD
    + ", njd=" + njd + ", qy=" + qy + ", Radar=" + Radar
    + ", temp=" + temp + ", cityid=" + cityid + ", city="
    + city + "]";
    }
    }
    }

    创建界面:

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. 将请求放入请求队列中去。
 public class JsonActivity extends Activity {
private static final String TAG = "com.soyoungboy.volleydemo.JsonActivity";
private RequestQueue mQueue;
private static final String WEATHER_LINK = "http://www.weather.com.cn/data/sk/101280101.html";
private TextView lvWeatherTv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
lvWeatherTv = (TextView) findViewById(R.id.lvWeatherTv);
mQueue = Volley.newRequestQueue(this);
getWeatherInfo();
}
public void getWeatherInfo() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
WEATHER_LINK, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (!TextUtils.isEmpty(response.toString())) {
WeathBean weathBean = WeathBean
.getBeanFromJson(response.toString());
lvWeatherTv.setText(weathBean.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
ToastUtils.toastL(getApplicationContext(),
error.getMessage());
}
});
mQueue.add(jsonObjectRequest);
}
}
显示结果:
【第一篇】Volley的使用之json请求